Task 6 Write a program for a basic calculator Using Switch Only Your calculator

Task 6 write a program for a basic calculator using

This preview shows page 10 - 16 out of 17 pages.

Task 6: Write a program for a basic calculator (Using Switch Only) Your calculator should taketwo integers. Then it should display options for different operations and then ask the user forchoice. Based on user’s choice it will perform the operation. The options will be displayed as
Image of page 10
int main() { int x, y,sum,diff,prod,division,remainder; char(choice); cout << "press 1 for addition"<<endl; cout << "Press 2 for subtraction"<<endl; cout << "Press 3 for multiplication" << endl; cout << "Press 4 for division" << endl; cout << "Press 5 for finding the mod" << endl; cout << "enter the first number:"; cin >> x; cout << "enter the second number:"; cin >> y; cout << "enter a choice:"; cin >> choice; switch (choice) { case '1':sum = x + y; cout << "sum=" <<sum<<endl; break; case'2':diff = x - y; cout << "diff=" <<diff << endl; break; case'3':prod = x*y; cout << "prod=" << prod << endl;
Image of page 11
break; case'4':division = x / y; cout << "division=" << division <<endl; break; case'5':remainder = x%y; cout << "remainder=" << remainder<<endl; break; } _getch(); return 0; } Output of Task 6: Sum:
Image of page 12
Difference: Product:
Image of page 13
Division: Modulous: Task 7: Now make the menu of the previous program stay on the screen until the user asks to exit using an infinite loop. For that simply enclose the whole code in while(1) loop, as shown in one of the practice tasks above and add a case 6: for exiting. Hint: Enclose its code in the following while block, Except the declaration and initialization statements. while (1) { system(“cls”); //adjust that code here, and add a 6 th case as shown below case 6: exit(0); }
Image of page 14
Code of task 7; #include <iostream> #include <conio.h> using namespace std; int main() { int num1, num2, Select, again; char E; while (1) {
Image of page 15
Image of page 16

You've reached the end of your free preview.

Want to read all 17 pages?

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture