You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> Enter length of cube: 4> The Volume of cube is 64cm3
4. Print sum, difference, product and quotient of two user input numbers.
#include<iostream>usingnamespacestd;classcalculator {
public:int a, b;
int sum = 0;
int diff;
int mul;
int rem;
voidreadInput() {
cout << "Enter any two number: ";
cin >> a;
cin >> b;
}
voidcalcSum() {
sum = a + b;
cout << "The sum is " << sum << endl;
}
voidcalcDiff() {
if(a > b) {
diff = a - b;
} else {
diff = b - a;
}
cout << "The difference is " << diff << endl;
}
voidcalcMul() {
mul = a * b;
cout << "The product is " << mul << endl;
}
voidcalcRem() {
rem = a / b;
cout << "The quotient is " << rem << endl;
}
};
intmain() {
calculator c1;
c1.readInput();
c1.calcSum();
c1.calcDiff();
c1.calcMul();
c1.calcRem();
return0;
}
Output
> Enter any two number: 10 2 > The sum is 12> The difference is 8> The product is 20> The quotient is 5
5. to input age of person and print in days with a appropriate format.
#include<iostream>usingnamespacestd;classageInDays {
public:int age;
int days;
int totalDays = 365;
voidreadAge() {
cout << "Enter your age: ";
cin >> age;
}
voidcalcAge() {
days = totalDays * age;
cout << "Age in days is " << days << " Days" << endl;
}
};
intmain() {
ageInDays age1;
age1.readAge();
age1.calcAge();
return0;
}
Output
> Enter you age: 19> Age in days is 6935 Days
6. To input length & breadth of a room and calculate and print its area and perimeter.
#include<iostream>usingnamespacestd;classroom {
public:int length;
int breath;
int area;
int perimeter;
voidreadData() {
cout << "Enter the length and breath of the room: ";
cin >> length;
cin >> breath;
}
voidcalcArea() {
area = length * breath;
cout << "The area of room is " << area << " cm2" << endl;
}
voidcalcPerimeter() {
perimeter = 2 * (length + breath);
cout << "The perimeter of room is " << perimeter << " cm";
}
};
intmain() {
room r1;
r1.readData();
r1.calcArea();
r1.calcPerimeter();
return0;
}
Output
> Enter the length and breath of the room: 10 20> The area of room is 200cm2> The perimeter of room is 60cm
7. To read the radius of a sphere and compute its surface area and volume.
#include<iostream>usingnamespacestd;classsphere {
public:doublepi = 3.14;
double radius;
double surfaceArea;
double volume;
voidreadRadius() {
cout << "Enter the radius of the sphere: ";
cin >> radius;
}
voidcalcSurfaceArea() {
surfaceArea = 4 * pi * radius * radius;
cout << "The surface area of sphere is " << surfaceArea << " cm2" << endl;
}
voidcalcVolume() {
volume = (4 / 3) * pi * radius * radius * radius;
cout << "The volume of sphere is " << volume << " cm3";
}
};
intmain() {
sphere sp1;
sp1.readRadius();
sp1.calcSurfaceArea();
sp1.calcVolume();
return0;
}
Output
> Enter the raduis of the sphere: 7> The surface area of sphere is 615.75 cm2> The volume of sphere is 1436.76 cm3
8. To input temperature in Celsius and to print its Fahrenheit equivalent.