Oops-Program 3-Mdu
Oops-Program 3-Mdu
cpp
PROGRAM NO. 3
Objective: - Create the equivalent of a four function calculator. The program should request the user
to enter a number, an operator, and another number. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. (It should use a switch statement to select the operation). Finally it should display the result. When it finishes the calculation, the program should ask if the user wants to do another calculation. The response can be Y or N. Some sample interaction with the program might look like this: Enter first number, operator, and second number: 10/ 3 Answer = 3.333333 Do another (Y/ N)? Y Enter first number, operator and second number 12 + 100 Answer = 112 Do another (Y/ N)? N
#include<iostream> using namespace std; int main() { float a,b,c; char x,z; do { cout<<"Enter first number,operator,second number:"; cin>>a>>x>>b; switch(x) { case '+':c=a+b; cout<<"Answer="<<c; break; case '-':c=a-b; cout<<"Answer="<<c; break; case '/':c=a/b; cout<<"Answer="<<c; break; case '*':c=a*b; cout<<"Answer="<<c; break; default :cout<<"wrong choice"; break; } cout<<"\nDo another(y/n)?:"; cin>>z; }while(z=='y'); return 0; }
//switch statement
//end of main()
SBIT/CSE/IV/C++ PROGRAMMING/CSE-206-F
CSE/10/409
OUTPUT
SBIT/CSE/IV/C++ PROGRAMMING/CSE-206-F
CSE/10/409