0% found this document useful (0 votes)
18 views2 pages

Cs 201 Assignment

The document contains the code for a C++ program that acts as a basic calculator menu. It allows the user to choose between addition, subtraction, multiplication, or division and then prompts the user to input numbers to perform the calculation and outputs the result.

Uploaded by

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

Cs 201 Assignment

The document contains the code for a C++ program that acts as a basic calculator menu. It allows the user to choose between addition, subtraction, multiplication, or division and then prompts the user to input numbers to perform the calculation and outputs the result.

Uploaded by

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

#include <iostream>

#include <cstdlib>

int main() {
std::cout << "Malika" << std::endl;
std::cout << "Student Id: BC230406326 Welcome to the main menu" << std::endl;

char choice;
do {
std::cout << "1. Addition" << std::endl;
std::cout << "2. Subtraction" << std::endl;
std::cout << "3. Multiplication" << std::endl;
std::cout << "4. Division" << std::endl;
std::cout << "5. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;

switch (choice) {
case '1': {
int count;
std::cout << "How many numbers do you want to add? ";
std::cin >> count;

int sum = 0;
for (int i = 0; i < count; ++i) {
int value;
std::cout << "Enter number " << i + 1 << ": ";
std::cin >> value;
sum += value;
}
std::cout << "Result of addition: " << sum << std::endl;
break;
}
case '2': {
int count;
std::cout << "How many numbers do you want to subtract? ";
std::cin >> count;

int result;
std::cout << "Enter number 1: ";
std::cin >> result;
for (int i = 1; i < count; ++i) {
int value;
std::cout << "Enter number " << i + 1 << ": ";
std::cin >> value;
result -= value;
}
std::cout << "Result of subtraction: " << result << std::endl;
break;
}
case '3': {
int count;
std::cout << "How many numbers do you want to multiply? ";
std::cin >> count;

int product = 1;
for (int i = 0; i < count; ++i) {
int value;
std::cout << "Enter number " << i + 1 << ": ";
std::cin >> value;
product *= value;
}
std::cout << "Result of multiplication: " << product << std::endl;
break;
}
case '4': {
int count;
std::cout << "How many numbers do you want to divide? ";
std::cin >> count;

double result;
std::cout << "Enter number 1: ";
std::cin >> result;
for (int i = 1; i < count; ++i) {
double value;
std::cout << "Enter number " << i + 1 << ": ";
std::cin >> value;
if (value != 0) {
result /= value;
} else {
std::cout << "Error: Cannot divide by zero!" << std::endl;
break; // Exit the loop on division by zero
}
}
std::cout << "Result of division: " << result << std::endl;
break;
}
case '5': {
std::cout << "Exiting the program." << std::endl;
break;
}
default:
std::cout << "Invalid choice. Please try again." << std::endl;
}

if (choice != '5') {
std::cout << "Do you want to continue (y/n)? ";
std::cin >> choice;
}
} while (choice == 'y' || choice == 'Y');

return 0;
}

You might also like