Untitled Document
Untitled Document
Programming
Question 1-C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using
switch...case
# include <iostream>
using namespace std;
int main() {
char op;
float num1, num2;
switch(op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
return 0;
}
Output
Enter operator: +, -, *, /: -
Enter two operands: 3.4 8.4
3.4 - 8.4 = -5
Question 2- calculate and display the area and volume of the room using oops
// Program to illustrate the working of
// objects and class in C++ Programming
#include <iostream>
using namespace std;
// create a class
class Room {
public:
double length;
double breadth;
double height;
double calculate_area() {
return length * breadth;
}
double calculate_volume() {
return length * breadth * height;
}
};
int main() {
return 0;
}
Output
int main() {
int n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n) {
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
Output