Tuesday, August 23, 2011

HOW TO MAKE A SIMPLE CALCULATOR IN C/C++

This is a simple calculator written in C++, it can ask user for two values and an operator (+,-,*,/)
based on the operator it will show the result, it also has a option to ask user if he wants to run the program again fmo the beginning.

 CODING
#include<iostream.h>
#include<conio.h>
main()
{
float numb1,numb2,ans;
char op,decision;
while (decision!='n')
{
clrscr();
cout<<"Enter your first number";
cin>>numb1;
cout<<"Enter your second number";
cin>>numb2;
cout<<"Enter operator (+,-,/,*)";
cin>>op;
switch(op)
{
    case '+':
    ans=numb1+numb2;
    cout<<"Your answer is "<<ans;
    break;

    case '-':
    ans=numb1-numb2;
    cout<<"Your answer is "<<ans;
    break;

    case '*':
    ans=numb1*numb2;
    cout<<"Your answer is "<<ans;
    break;

    case '/':
    ans=numb1/numb2;
    cout<<"Your answer is "<<ans;
    break;

    default:
    cout<<"You entered wrong operator";
}
cout<<"\nDo you want to run this program again ?";
cin>>decision;
}
}

No comments:

Post a Comment