/*1.
A special two-digit number is such that when the sum of its digits is added to the product
of its digits, the result is equal to the original two-digit number.
Example: Consider the number 59. Sum of digits = 5 + 9 = 14 Product of its digits = 5 x 9 =
45 Sum of the sum of digits and product of digits= 14 + 45 = 59
Implement a C++ program to accept a two-digit number. Add the sum of its digits to the
product of its digits. If the value is equal to the number input, output the message “Special 2-
digit number” otherwise, output the message “Not a Special 2-digit number”.*/
#include<iostream>
using namespace std;
int main()
{
int num,num1,sum=0,prod=1,c=0,rem;
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----INPUT-----\n";
cout<<"Enter the 2-digit number:";
cin>>num;
num1=num;
while(num1>0)
{
rem=num1%10;
sum=sum+rem;
prod=prod*rem;
c++;
num1=num1/10;
}
cout<<"-----OUTPUT-----\n";
if(c==2)
{
if((sum+prod)==num)
{
cout<<"Special 2-digit number";
}
else
cout<<"Not a special 2-digit number";
}
else
cout<<"Not double digit";
}
/*2.Using the switch statement, write a menu driven program
(i) To check and display whether a number input by the user is a composite number or not (A
number is said to be a composite, if it has one or more than one factors excluding 1 and the
number itself).
Example: 4, 6, 8, 9…
(ii) To find the smallest digit of an integer that is input:
Sample input: 6524
Sample output: Smallest digit is 2
For an incorrect choice, an appropriate error message should be displayed.*/
#include<iostream>
using namespace std;
int main()
{
int ch,n,c=0,rem,min,i;
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"1.To check composite number\n2.To find smallest digit\n";
cin>>ch;
switch(ch)
{
case 1:
cout<<"-----INPUT-----\n";
cout<<"Enter number:";
cin>>n;
for(i=1;i<n;i++)
{
if(n%i==0)
{
c++;
break;
}
}
cout<<"-----OUTPUT-----\n";
if(c>1)
cout<<"Composite number"<<endl;
else
cout<<"Not composite"<<endl;
break;
case 2:
cout<<"-----INPUT-----\n";
cout<<"Enter number:";
cin>>n;
rem=n%10;
min=rem;
n=n/10;
while(n>0)
{
rem=n%10;
if(min>rem)
{
min=rem;
}
n=n/10;
}
cout<<"-----OUTPUT-----\n";
cout<<"Smallest digit is:"<<min<<endl;
break;
default:
cout<<"Wrong choice";
break;
}
}
/*3.Implement a C++ program to input five words from the console and remove the
consecutive repeated characters by replacing the sequence of repeated characters by its single
occurrence of each words.
Sample Example:
INPUT – PROOOGGRAMMMMIIING
OUTPUT – PROGRAMING
INPUT – HowwwaareeyooUU
OUTPUT – HowareyoU*/
#include <iostream>
using namespace std;
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
string str;
int i,j,len,len1;
//input string
cout<<"Enter any string: ";
getline(cin,str);
//calculating length
for(len=0; str[len]!='\0'; len++);
len1=0;
//Removing consecutive repeated characters from string
for(i=0; i<(len-len1);)
{
if(str[i]==str[i+1])
{
//shift all characters
for(j=i;j<(len-len1);j++)
str[j]=str[j+1];
len1++;
}
else
{
i++;
}
}
cout<<"String after removing characaters:"<<str;
return 0;
}
/*4. Define a class in C++ with following description:
Private Members :
A data member Flight number of type integer
A data member Destination of type string
A data member Distance of type float
A data member Fuel of type float
A member function CALFUEL() to calculate the value of Fuel as per the following criteria
Distance Fuel
<=1000 500
more than 1000 and <=2000 1100
more than 2000 2200
Public Members :
A function FEEDINFO() to allow user to enter values for Flight Number, Destination,
Distance & call function CALFUEL() to calculate the quantity of Fuel A function
SHOWINFO() to allow user to view the content of all the data members.*/
#include<iostream>
using namespace std;
class flight
{
private:
int flightno;
string destination;
float distance,fuel;
public:
void FEEDINFO()
{
cout<<"-----INPUT-----"<<endl;
cout<<"Input flight number "<<endl;
cin>>flightno;
cout<<"Input destination"<<endl;
cin>>destination;
cout<<"Input distance "<<endl;
cin>>distance;
}
void CALFUEL()
{
if(distance<=1000)
{
fuel=500;
}
if(distance>1000&&distance<=2000)
{
fuel=1100;
}
if(distance>2000)
{
fuel=2200;
}
}
void SHOWINFO()
{
cout<<"-----OUTPUT-----"<<endl;
cout<<"Flight number
="<<flightno<<endl<<"Destination="<<destination<<endl<<"Distance="<<distance<<endl<
<"Fuel="<<fuel<<endl;
}
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
flight ob;
ob.FEEDINFO();
ob.CALFUEL();
ob.SHOWINFO();
}
/*5. Imagine a tollbooth with a class called TollBooth. The two data items are
of type int and double to hold the total number of cars and total amount of
money collected. A constructor initializes both of these data members to O. A
member function called payingCar( ) increments the car total and adds 0.5 to
the cash total. Another function called nonPayCar( ) increments the car total
but adds nothing to the cash total. Finally a member function called display( )
shows the two totals. Include a program to test this class. This program
should allow the user to push one key to count a paying car , and another to
count a non paying car. Pushing the 'E' key should cause the program to print
out the total number of cars and total cash and then exit*/
#include<iostream>
using namespace std;
class toolbooth
{
private:
int car;
double tamt;
public:
toolbooth()
{
car=0;
tamt=0;
}
void payingcar()
{
car++;
tamt=tamt+0.5;
}
void nonpayingcar()
{
car++;
}
void display()
{
cout<<"Total cars = "<<car<<endl;
cout<<"Total amount = "<<tamt<<endl;
}
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
toolbooth ob;
int a;
char n;
do
{
cout<<"Input 'a' for paying car , 'b' for non paying cars , 'E' for total cars and total
amount "<<endl;
cin>>n;
switch(n)
{
case 'a':
ob.payingcar();
break;
case 'b':
ob.nonpayingcar();
break;
case 'E':
ob.display();
break;
}
cout<<"1=continue\n0=stop"<<endl;
cin>>a;
}while(a==1);}
/*6. Implement a class called Time that has separate int member data for hours, minutes and
seconds. One constructor should initialize this data to 0, and another should initialize it to
fixed values. A member function should display it in 11:59:59 format. A member function
named add() should add two objects of type time passed as arguments. A main ( ) program
should create two initialized values together, leaving the result in the third time variable.
Finally it should display the value of this third variable.*/
#include <iostream>
using namespace std;
class Time
{
public:
int hours,minutes,seconds;
Time()
{
hours=minutes=seconds=0;
}
Time(int hours,int minutes,int seconds)
{
this->hours=hours;
this->minutes=minutes;
this->seconds=seconds;
}
void show()
{
cout<<"Time :- "<<hours<<":"<<minutes<<":"<<seconds;
}
void addTime(Time x,Time y)
{
seconds=x.seconds+y.seconds;
if(seconds>59)
{
seconds=seconds-60;
minutes++;
}
minutes=minutes+x.minutes+y.minutes;
if(minutes>59)
{
minutes=minutes-60;
hours++;
}
hours=hours+x.hours+y.hours;
}
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
Time T1(6,30,25),T2(3,30,45),T3;
cout<<"-----OUTPUT-----"<<endl;
T3.addTime(T1,T2);
T3.show();
return 0;
}
/*7. Implement a c++ program to create a class called ConsDemo and overload SumDemo ()
function.
SumDemo (int, char):If passing character is ‘p’ then print square of passing number
otherwise cube of a number
SumDemo (int, int, char):if passing character is ‘a’ then print addition of numbers otherwise
print Ascii value of a passing character.
SumDemo (string, string)->Check whether passing strings are equal or not.*/
#include<iostream>
using namespace std;
class ConsDemo
{
public:
void SumDemo(int x,char a)
{
if(a=='p')
{
cout<<"Square of "<<x<<" is "<<x*x<<endl;
}
else
{
cout<<"Cube of "<<x<<"is "<<x*x*x<<endl;
}
}
void SumDemo(int x,int y,char a)
{
if(a=='a')
{
cout<<"Addition of "<<x<<" and "<<y<<" is "<<x+y<<endl;
}
else
{
cout<<"Ascii value of a passing character is "<<(int)a<<endl;
}
}
void SumDemo(string str1,string str2)
{
if(str1.compare(str2)==0)
{
cout<<"Strings are equal"<<endl;
}
else
{
cout<<"Strings are not equal"<<endl;
}
}
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
ConsDemo ob;
int x,y;
char a;
string str1,str2;
cout<<"Enter a number and a character:";
cin>>x>>a;
ob.SumDemo(x,a);
cout<<"Enter two numbers and a character:";
cin>>x>>y>>a;
ob.SumDemo(x,y,a);
cin.ignore();
cout<<"Enter two strings:";
getline(cin,str1);
getline(cin,str2);
ob.SumDemo(str1,str2);
return 0;
}
/*8.Define a class named UserOne Description
with following description: Data
Members
Name To store person name
FatherName To store Father Name
MotherName To store Mother Name
Gender To store gender
Member Functions Description
InputInfo() Input Name, Father name and
Mother
name and gender
Friend Function Description
Userchecker(UserOne,UserTwo) Compare data */
#include<iostream>
using namespace std;
class usertwo;
class userone
{
string name,f_name,m_name,gender;
public:
void inputinfo()
{
cout<<"enter your name:";
getline(cin,name);
cout<<"enter your father's name:";
getline(cin,f_name);
cout<<"enter your mother's name:";
getline(cin,m_name);
cout<<"enter your gender:";
getline(cin,gender);
cout<<endl;
}
friend void userchecker(userone&,usertwo&);
};
class usertwo
{
string name,f_name,m_name,gender;
public:
void inputinfo()
{
cout<<"enter your name:";
getline(cin,name);
cout<<"enter your father's name:";
getline(cin,f_name);
cout<<"enter your mother's name:";
getline(cin,m_name);
cout<<"enter your gender:";
getline(cin,gender);
cout<<endl;
}
friend void userchecker(userone&,usertwo&);
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----INPUT-----"<<endl;
userone ob1;
ob1.inputinfo();
usertwo ob2;
ob2.inputinfo();
userchecker(ob1,ob2);
}
void userchecker(userone &ot1,usertwo &ot2)
{
cout<<"-----OUTPUT-----"<<endl;
if((ot1.f_name.compare(ot2.f_name)==0)&&(ot1.m_name.compare(ot2.m_name))==0)
{
cout<<"belongs to same family"<<endl;
if((ot1.gender=="male")&&(ot2.gender=="male"))
{
cout<<"we are brothers";
}
else if((ot1.gender=="female")&&(ot2.gender=="female"))
{
cout<<"we are sisters";
}
else
{
cout<<"we are brother and sister";
}
}
else
{
cout<<"belongs to different family";
}}
/*9. Using the concept of operator overloading.Write a program to overload using with and
without friend Function.
a. Unary –
b. Unary ++ preincrement, postincrement
c. Unary -- predecrement, postdecrement */
//using friend function
#include <iostream>
using namespace std;
class Unaryfriend
{
public:
int x;
void InputInfo()
{
cout<<"Enter the number x :- ";
cin>>x;
}
friend void operator-(Unaryfriend &U1);
friend Unaryfriend operator--(Unaryfriend &U2);
friend Unaryfriend operator--(Unaryfriend &U3,int);
friend Unaryfriend operator++(Unaryfriend &U4);
friend Unaryfriend operator++(Unaryfriend &U5,int);
};
void operator-(Unaryfriend &U1)
{
U1.x=-U1.x;
}
Unaryfriend operator--(Unaryfriend &U1)
{
Unaryfriend U2;
U2.x=--U1.x;
return U2;
}
Unaryfriend operator--(Unaryfriend &U1,int)
{
Unaryfriend U2;
U2.x=U1.x;
--U1.x;
return U2;
}
Unaryfriend operator++(Unaryfriend &U1)
{
Unaryfriend U2;
U2.x=++U1.x;
return U2;
}
Unaryfriend operator++(Unaryfriend &U1,int)
{
Unaryfriend U2;
U2.x=U1.x;
++U1.x;
}
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
Unaryfriend U1;
U1.InputInfo();
cout<<"Before overloading Unary- operator:- "<<U1.x<<endl;
-U1;
cout<<"After overloading Unary- operator :- "<<U1.x<<endl;
Unaryfriend U2,U3;
U2.InputInfo();
cout<<"Before overloading predecrement operator :- "<<U2.x<<endl;
U3=--U2;
cout<<"After overloading predecrement operator :- "<<U3.x<<endl;
Unaryfriend U4,U5;
U4.InputInfo();
cout<<"Before overloading postdecrement operator :- "<<U4.x<<endl;
U5=U4--;
cout<<"After overloading postdecrement operator :- "<<U5.x<<endl;
Unaryfriend U6,U7;
U6.InputInfo();
cout<<"Before overloading preincrement operator :- "<<U6.x<<endl;
U7=++U6;
cout<<"After overloading preincrement operator :- "<<U7.x<<endl;
Unaryfriend U8,U9;
U8.InputInfo();
cout<<"Before overloading postincrement operator :- "<<U8.x<<endl;
U9=U8++;
cout<<"After overloading postincrement operator :- "<<U9.x<<endl;
}
//without friend function
#include <iostream>
using namespace std;
class Unaryfriend
{
public:
int x;
void InputInfo()
{
cout<<"Enter the number x :- ";
cin>>x;
}
void operator-()
{
x=-x;
}
Unaryfriend operator--()
{
Unaryfriend U2;
U2.x=--x;
return U2;
}
Unaryfriend operator--(int)
{
Unaryfriend U2;
U2.x=x;
--x;
return U2;
}
Unaryfriend operator++()
{
Unaryfriend U2;
U2.x=++x;
return U2;
}
Unaryfriend operator++(int)
{
Unaryfriend U2;
U2.x=x;
++x;
return U2;
}
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
Unaryfriend U1;
U1.InputInfo();
cout<<"Before overloading Unary- operator:- "<<U1.x<<endl;
-U1;
cout<<"After overloading Unary- operator :- "<<U1.x<<endl;
Unaryfriend U2,U3;
U2.InputInfo();
cout<<"Before overloading predecrement operator :- "<<U2.x<<endl;
U3=--U2;
cout<<"After overloading predecrement operator :- "<<U3.x<<endl;
Unaryfriend U4,U5;
U4.InputInfo();
cout<<"Before overloading postdecrement operator :- "<<U4.x<<endl;
U5=U4--;
cout<<"After overloading postdecrement operator :- "<<U5.x<<endl;
Unaryfriend U6,U7;
U6.InputInfo();
cout<<"Before overloading preincrement operator :- "<<U6.x<<endl;
U7=++U6;
cout<<"After overloading preincrement operator :- "<<U7.x<<endl;
Unaryfriend U8,U9;
U8.InputInfo();
cout<<"Before overloading postdecrementincrement operator :- "<<U8.x<<endl;
U9=U8++;
cout<<"After overloading postincrement operator :- "<<U9.x<<endl;
}
/*10. Create a class Complex having two int type variable named real & img denoting real
and imaginary part respectively of a complex number. Overload + operator to add two
complex numbers
a. With friend function
b. Without friend function */
//using friend function
#include <iostream>
using namespace std;
class Complex
{
public:
int real,img;
void InputInfo()
{
cout<<"Enter real part :- ";
cin>>real;
cout<<"Enter imaginary part :- ";
cin>>img;
}
friend Complex operator+(Complex,Complex);
};
Complex operator+(Complex C1,Complex C2)
{
Complex C3;
C3.real=C1.real+C2.real;
C3.img=C1.img+C2.img;
return C3;
}
int main()
{
Complex C1,C2,C3;
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----INPUT-----:"<<endl;
cout<<"Enter InputInfo for first number :- "<<endl;
C1.InputInfo();
cout<<"Enter InputInfo for second number :- "<<endl;
C2.InputInfo();
C3=C1+C2;
cout<<"-----OUTPUT------"<<endl;
cout<<"Sum of Complex Number is :- "<<C3.real<<"+i"<<C3.img;
return 0;
}
//without using friend function
#include <iostream>
using namespace std;
class Complex
{
public:
int real,img;
void InputInfo()
{
cout<<"Enter real part :- ";
cin>>real;
cout<<"Enter imaginary part :- ";
cin>>img;
}
Complex operator+(Complex C2)
{
Complex C3;
C3.real=this->real+C2.real;
C3.img=this->img+C2.img;
return C3;
}
};
int main()
{
Complex C1,C2,C3;
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----INPUT-----"<<endl;
cout<<"Enter InputInfo for first number :- "<<endl;
C1.InputInfo();
cout<<"Enter InputInfo for second number :- "<<endl;
C2.InputInfo();
C3=C1+C2;
cout<<"-----OUTPUT-----"<<endl;
cout<<"Sum of Complex Number is :- "<<C3.real<<"+i"<<C3.img;
return 0;
}
/*11. Implement a class called Student that contains the data members like age, name,
enroll_no, marks. Create another class called Faculty that contains data members like
facultyName, facultyCode, salary, deptt, age, experience, gender. Create the function
display() in both the classes to display the respective information. The derived Class Person
demonstrates multiple inheritance. The program should be able to call both the base classes
and displays their information. Remove the ambiguity (When we have exactly same variables
or same methods in both the base classes, which one will be called?) by proper mechanism.*/
#include <iostream>
using namespace std;
class Student
{
public:
int age,marks,enroll_no;
string name;
Student()
{
}
void Inputinfostudent(int age,int marks,int enroll_no,string name)
{
this->age=age;
this->marks=marks;
this->enroll_no=enroll_no;
this->name=name;
}
void display()
{
cout<<"Name :- "<<name<<endl;
cout<<"Age :- "<<age<<endl;
cout<<"Marks :- "<<marks<<endl;
cout<<"Enroll_no :- "<<enroll_no<<endl;
}
};
class Faculty
{
public:
string facultyname,deptt,gender;
int age,salary,facultycode;
Faculty()
{
}
void Inputinfofaculty(string facultyname,string deptt,string gender,int age,int salary,int
facultycode)
{
this->facultyname=facultyname;
this->deptt=deptt;
this->gender=gender;
this->age=age;
this->salary=salary;
this->facultycode=facultycode;
}
void display()
{
cout<<"Facultyname :- "<<facultyname<<endl;
cout<<"Gender :- "<<gender<<endl;
cout<<"salary :- "<<salary<<endl;
cout<<"Facultycode :- "<<facultycode<<endl;
cout<<"Deptt :- "<<deptt<<endl;
cout<<"Age :- "<<age<<endl;
}
};
class Person:public Student,public Faculty
{
};
int main()
{
Person p;
p.Inputinfostudent(19,99,2018,"Aishna");
p.Inputinfofaculty("Ram","CS","Male",30,100000,1001);
cout<<"-----OUTPUT------"<<endl;
cout<<"Display function of student"<<endl;
p.Student::display();
cout<<"Display function of faculty"<<endl;
p.Faculty::display();
return 0;
}
/*12. Use virtual base class inheritance to resolve the following dreaded diamond problem.
Student class having data members:
Protected: roll no ,name
Sports class having data members:
Protected: sp1,sp2,sp3(Sports marks)
Academic class having data members:
Protected: m1,m2,m3,m4,m5( marks in 5 subjects)
Result class have the following member function:
Private:
total_marks;
Public:
Total(): it will sum up the academic marks and sports marks.
display()- it displays the information of the student like his roll,name,total marks*/
#include <iostream>
using namespace std;
class Student
{
protected:
int roll_no;
string name;
public:
void InputInfo()
{
cout<<"Enter the name :- ";
getline(cin,name);
cout<<"Enter the roll no :- ";
cin>>roll_no;
}
};
class Sports:virtual public Student
{
protected:
int sp1,sp2,sp3;
public:
void InputInfo()
{
cout<<"Enter the sports marks:- "<<endl;
cin>>sp1>>sp2>>sp3;
}
};
class Academic:virtual public Student
{
protected:
int m1,m2,m3,m4,m5;
public:
void InputInfo()
{
cout<<"Enter the Academic marks :- "<<endl;
cin>>m1>>m2>>m3>>m4>>m5;
}
};
class Result:public Sports,public Academic
{
private:
int total_marks;
public:
void InputInfo()
{
Student::InputInfo();
Sports::InputInfo();
Academic::InputInfo();
}
void Total()
{
total_marks=sp1+sp2+sp3+m1+m2+m3+m4+m5;
}
void display()
{
cout<<"Name :- "<<name<<endl;
cout<<"Roll no :- "<<roll_no<<endl;
cout<<"Total marks :- "<<total_marks<<endl;
}
};
int main()
{
Result R;
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----INPUT-----"<<endl;
R.Student::InputInfo();
R.Sports::InputInfo();
R.Academic::InputInfo();
R.Total();
cout<<"-----OUTPUT-----"<<endl;
R.display();
return 0;
}
/*13. Create a base class called shape. Use this class to store two double type values that
could be used to compute the area of figures. Derive two specific classes called triangle and
rectangle from base shape. Add to the base class , a member function get_data() to initialize
base class data members and another member function display_area() to compute and
display the area of figures. Make display_area() as a virtual function and redefine this
function in the derived class to suit their requirements. Using these three classes, design a
program that will accept dimensions of a triangle or a rectangle interactively and display the
area.Remember the two values given as input will be treated as lengths of two sides in the
case of rectangles and as base and height in the case of triangle and used as follows:
Area of rectangle = x * y Area of triangle = ½ *x*y */
#include <iostream>
using namespace std;
class shape
{
public:
double x,y;
void getdata()
{
cout<<"Enter one number :- ";
cin>>x;
cout<<"Enter second number :- ";
cin>>y;
}
virtual void displayarea()=0;
};
class triangle:public shape
{
public:
void displayarea()
{
cout<<"area of triangle is :- "<<(1.0/2)*x*y<<endl;
}
};
class rectangle:public shape
{
public:
void displayarea()
{
cout<<"area of rectangle is :- "<<x*y<<endl;
}
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
triangle t;
rectangle r;
cout<<"-----INPUT-----"<<endl;
cout<<"for triangle :- "<<endl;
t.getdata();
cout<<"for rectangle :- "<<endl;
r.getdata();
cout<<"-----OUTPUT-----"<<endl;
t.displayarea();
r.displayarea();
return 0;}
/*14. Implemente a C++ program to implement pure virtual function with
following details:
Create A Base Class Temperature
Data members:
Float temp;
Function members
void setTempData(float)
virtual void changetemp()=0;
Sub Class Fahrenheit (subclass of Temperature)
Data members:
Float ctemp;
Function members
Override function changetemp() to convert Fahrenheit temperature into degree
Celsius by using formula C=5/9*(F-32) and display converted temperature
Sub Class Celsius (subclass of Temperature)
Data members:
Float ftemp;
Function members
Override function changetemp() to convert degree Celsius into Fahrenheit
temperature by using formula F=9/5*c+32 and display converted temperature*/
#include <iostream>
using namespace std;
class Temperature
{
public:
float temp;
void setTempData(float temp)
{
this->temp=temp;
}
virtual void changetemp()=0;
};
class Fahrenheit:public Temperature
{
public:
float ctemp;
void changetemp()
{
ctemp=5.0/9*(temp-32);
cout<<"Converted temp in celsius :- "<<ctemp<<endl;
}
};
class Celsius:public Temperature
{
public:
float ftemp;
void changetemp()
{
ftemp=((9/5.0)*temp)+32;
cout<<"Converted temp in Fahrenheit :- "<<ftemp<<endl;
}
};
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
int Far,Cel;
Fahrenheit f;
Celsius c;
cout<<"-----INPUT-----"<<endl;
cout<<"Enter Temperature in Fahrenheit :- ";
cin>>Far;
cout<<"Enter Temperature in Celsius :- ";
cin>>Cel;
cout<<endl;
cout<<"-----OUTPUT-----"<<endl;
f.setTempData(Far);
f.changetemp();
c.setTempData(Cel);
c.changetemp();
}
/*15. Implement a C++ program to read three numbers x, y and z and evaluate R given by R
= z/(x-y). Use exception handling to throw an exception in case division by zero is
attempted.*/
#include<iostream>
#include<stdexcept>
using namespace std;
float division(float z,float x,float y)
{
if((x-y)==0)
{
throw runtime_error("Math error:Attempted to divide by Zero\n");
}
return(z/x-y);
}
int main()
{
float x,y,z,R;
cout<<"Input value of x , y and z"<<endl;
cin>>x>>y>>z;
try
{
R=division(z,x,y);
cout<<"The quotient is "<<R<<endl;
}
catch(runtime_error& e)
{
cout<<"Exception occured"<<endl<<e.what();
}
}
/*16. Implement a C++ program to write set of characters to a file stored in a secondary
storage. Again, read through a file the same set of characters and display it on the console by
the use of stream classes.
Input: hello graphic era university
Output: hello graphic era university*/
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----INPUT-----"<<endl;
fstream file;
file.open("sample_file.txt", ios_base::out);
if(!file.is_open())
{
cout<<"Unable to open the file.\n";
return 0;
}
string str;
getline(cin,str);
file<<str;
file.close();
cout<<"-----OUTPUT-----"<<endl;
file.open("sample_file.txt",ios::in);
if (file.is_open())
{
string str;
while(getline(file,str))
{
cout <<str<<"\n";
}
file.close();
}
return 0;
}
/*17 Implement with the help of STL the following classes
a. List
b. Vector*/
a)#include <iostream>
#include<iterator>
#include <list>
using namespace std;
void showlist(list<int> l)
{
list<int>::iterator it;
for(it =l.begin();it !=l.end();++it)
{
cout<<*it<<"\t";
}
}
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----Input-----"<<endl;
list<int>l;
int ele;
cout<<"Enter number of elements ";
cin>>ele;
for(int i=0;i<ele;i++)
{
l.push_back(i*2);
}
cout<<"-----Output-----"<<endl;
showlist(l);
}
b) #include <iostream>
#include <vector>
using namespace std;
int main()
{
cout<<"Name:Aishna Pathak\nUniversity roll no.:2018645\nSec:E"<<endl;
cout<<"-----Input-----"<<endl;
int ele;
vector<int> ve;
cout<<"Enter number of elements ";
cin>>ele;
for(int i=0;i<ele;i++)
{
ve.push_back(i);
}
cout<<"-----Output-----"<<endl;
for(int i=0;i<ve.size();i++)
{
cout<<ve[i]<<"\t";
}
return 0;
}