0% found this document useful (0 votes)
23 views

CS201 Solution Code

Uploaded by

M ZOHAIB JUTT
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

CS201 Solution Code

Uploaded by

M ZOHAIB JUTT
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

using namespace std;

int main() {
const string studentID = "Bc210411285";
const string studentName = "Abdullah";

cout << "Hello " << studentName << ", Student Id=" << studentID << " Welcome to
the Main Menu\n";

int choice;
float num, result = 0;
char cont;

do {
cout << "\n1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Division\n";
cout << "5. Exit\n";
cout << "Enter your choice: ";
cin >> choice;

switch(choice) {
case 1:
cout << "\nHow many numbers do you want to add? ";
int n_add;
cin >> n_add;
result = 0;
for(int i = 0; i < n_add; i++) {
cout << "Enter number " << i + 1 << ": ";
cin >> num;
result += num;
}
cout << "Result of addition: " << result << endl;
break;
case 2:
cout << "\nHow many numbers do you want to subtract? ";
int n_sub;
cin >> n_sub;
cout << "Enter number 1: ";
cin >> result;
for(int i = 0; i < n_sub - 1; i++) {
cout << "Enter number " << i + 2 << ": ";
cin >> num;
result -= num;
}
cout << "Result of subtraction: " << result << endl;
break;
case 3:
cout << "\nHow many numbers do you want to multiply? ";
int n_mul;
cin >> n_mul;
result = 1;
for(int i = 0; i < n_mul; i++) {
cout << "Enter number " << i + 1 << ": ";
cin >> num;
result *= num;
}
cout << "Result of multiplication: " << result << endl;
break;
case 4:
cout << "\nHow many numbers do you want to divide? ";
int n_div;
cin >> n_div;
cout << "Enter number 1: ";
cin >> result;
for(int i = 0; i < n_div - 1; i++) {
cout << "Enter number " << i + 2 << ": ";
cin >> num;
if(num == 0) {
cout << "Error! Division by zero is not allowed." << endl;
break;
}
result /= num;
}
cout << "Result of division: " << result << endl;
break;
case 5:
cout << "\nExiting program. Goodbye!\n";
break;
default:
cout << "\nInvalid choice. Please try again.\n";
}
if (choice != 5) {
cout << "\nDo you want to continue? (y/n): ";
cin >> cont;
}
} while(choice != 5 && (cont == 'y' || cont == 'Y'));

return 0;
}

You might also like