0% found this document useful (0 votes)
3 views3 pages

Untitled Document

The document presents C++ programming exercises including a simple calculator using switch-case, a class to calculate the area and volume of a room, and a recursive function to compute the factorial of a number. Each question includes code snippets and sample output demonstrating the functionality. The exercises illustrate fundamental programming concepts such as operators, classes, and recursion in C++.

Uploaded by

ambikasingh63942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Untitled Document

The document presents C++ programming exercises including a simple calculator using switch-case, a class to calculate the area and volume of a room, and a recursive function to compute the factorial of a number. Each question includes code snippets and sample output demonstrating the functionality. The exercises illustrate fundamental programming concepts such as operators, classes, and recursion in C++.

Uploaded by

ambikasingh63942
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

BCA-2001P Computer Laboratory and Practical Work of C++

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;

cout << "Enter operator: +, -, *, /: ";


cin >> op;

cout << "Enter two operands: ";


cin >> 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() {

// create object of Room class


Room room1;

// assign values to data members


room1.length = 42.5;
room1.breadth = 30.8;
room1.height = 19.2;

// calculate and display the area and volume of the room


cout << "Area of Room = " << room1.calculate_area() << endl;
cout << "Volume of Room = " << room1.calculate_volume() << endl;

return 0;
}
Output

Area of Room = 1309


Volume of Room = 25132.8

Question 3-Calculate Factorial Using Recursion


#include<iostream>
using namespace std;

int factorial(int n);

int main() {

int n;

cout << "Enter a positive integer: ";


cin >> 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

Enter an positive integer: 6


Factorial of 6 = 720

You might also like