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;
}
}

1 comment:

  1. Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
    Be with us to learn programming in new and creative way.

    ReplyDelete