Lab 5
{
int num1,num2;
char op;
cout << "Enter First number=";
cout<<"\n";
cin >>num1;
cout << "Enter Second number=";
cout<<"\n";
cin >> num2;
cout << "Enter the arithmetic operator";
cout<<"\n";
cin >> op;
switch(op)
{
case '+':
cout << num1+num2;
break;
case '-':
cout << num1-num2;
break;
case '*':
cout << num1*num2;
break;
case '/':
cout << num1/num2;
break;
case'%':
cout<<num1%num2;
break;
default:
cout << "Error! operator is not correct";
}
getch();
}
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:
Introduction to Computer Programming Lab
Page 7

Lab 5
exit(0);
}
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
char op;
cout<<"Enter a first number\n";
cin>>a;
cout<<"Enter a second number\n";
cin>>b;
cout<<"Enter a operator\n";
cin>>op;
while(1)
{
system("cls");
switch (op)
{
case 1: '+';
cout<<"Addition is="<<a+b;
break;
case 2: '-';
cout<<"Subtraction is="<<a-b;
break;
case 3: '*';
cout<<"Multiplication is="<<a*b;
break;
case 4: '/';
cout<<"Divison is="<<a/b;
break;
case 5: '%';
cout<<"Remiander is="<<a%b;
break;
case 6:
exit (0);
break;
}
}
getch();
}
****************************************************
Introduction to Computer Programming Lab
Page 8

You've reached the end of your free preview.
Want to read all 8 pages?
- Fall '19