Saturday, January 26, 2013

Write a C++ program to MAke Simple calculator


Exercise 1)
Write a program and call it calc.cpp which is the basic calculator and receives three values from input via keyboard.
 The first value as an operator (Op1) should be a char type and one of (+, -, *, /, s) characters with the following meanings:
o ‘+’ for addition (num1 + num2)
o ‘-’ for subtraction (num1 - num2)
o ‘*’ for multiplication (num1 * num2)
o ‘/’ for division (num1 / num2)
o ‘s’ for swap
 Program should receive another two operands (Num1, Num2) which could be float or integer.
 The program should apply the first given operator (Op1) into the operands (Num1, Num2) and prints the relevant results with related messages in the screen.
 Swap operator exchanges the content (swap) of two variables, for this task you are not allowed to use any further variables (You should use just two variables to swap).

#include<iostream>
#include<cmath>

using namespace std;

int main()
{
//-------defining variables and initializing them-------------    
    double num1,num2;
    char operation,redo;
//--------Printing my name on screen----------------    
    cout<<"Welcome to the calculater program v.1.0 written by Your Name"<<endl;
    cout<<"***************************************************************"<<endl;
    cout<<endl<<endl<<endl;
//--here do loop is used so that the program can be used more then one time
//without exiting the run screen---------------------------    
    do
    {
 //----receiving the variables from input--------------         
    cout<<" Please enter an operation which you like to calculate (+,-,*,/,s)";
    cout<<"[s stands for swap]:";
    cin>>operation ;
    cout<<endl<<endl;
     cout<<" Please enter two numbers to apply your requested operation(";
    cout<<operation<<"):"<<endl<<"1st num:";
    cin>>num1;
    cout<<"2nd num:" ;
    cin>>num2;
    cout<<endl;
 //---used switch function so thet the operater can be decided------------  
    switch (operation)
    {
//------calculating the requested equation for inputs------------- 
//-------at the same time printing the results on screen-----------          
     case'+':            
             cout<<"The addition of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1+num2<<endl;
             break; 
     case'-':
             cout<<"The substraction of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1-num2<<endl;
             break;
      case'*':
             cout<<"The multiplication of two numbers ("<<num1<<","<<num2<<"):";
             cout<<num1*num2<<endl;
             break;
      case'/':
             cout<<"The division of two numbers ("<<num1<<","<<num2<<"):";
             if(num2==0)
             {
             cout<<"not valid"<<endl;
             }
             cout<<(num1/num2)<<endl;
             break;
      case's':
             cout<<"The swap of two numbers ("<<num1<<","<<num2<<"):";
             swap(num1,num2);
             cout<<"1stnumber="<<num1<<"and 2nd number="<<num2<<endl<<endl;                    
           break;
      default:
              cout<<"unknown command"<<endl;
      
           }
 //----now once again the program will ask the user if want to continue or not          
           cout<<"enter y or Y to continue:";
           cin>>redo;
           cout<<endl<<endl;
           }
           while(redo=='y'||redo=='Y');
           
   system("pause");
    return 0;
    
    }



C++ Calculator Program - Source Code





---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------



---------------------------------------------------------------------------------------------
---------------------------------------------------------------------------------------------




C++ Calculator Program
C++ Beginner Calculator Code 
C++ calculator - C++ - Source Code
Simple C++ Calculator - C++ Tutorials
Simple Calculator in C++ - Beginners
homework - Simple calculator program in C/C++ 
How to program a calcuator in C++
Problem with Calculator Program - C++ 
Write a C++ program that mimics a calculator
Calculator in C++ Language 
My Calculator

14 comments:

  1. this one is too long,,,i got the simplest from:

    http://www.programmingtunes.com/generating-prime-numbers-c/

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. does switch only work for characters or can it work with something like case 'x>1'

    ReplyDelete
  4. TRY THIS CALCULATOR I HOPE YOU IKE THIS;

    #include
    #include
    #include
    #include
    void main()
    {
    int gd=DETECT,gm,x,y,s;
    initgraph(&gd,&gm,"c:\\turboc3\\bgi");
    rectangle(159,50,491,450);

    rectangle(180,70,469,130);

    rectangle(180,200,240,250);
    rectangle(250,200,310,250);
    rectangle(320,200,380,250);
    rectangle(400,200,469,250);

    rectangle(180,260,240,310);
    rectangle(250,260,310,310);
    rectangle(320,260,380,310);
    rectangle(400,260,469,310);

    rectangle(180,320,240,370);
    rectangle(250,320,310,370);
    rectangle(320,320,380,370);
    rectangle(400,320,469,370);

    rectangle(180,380,310,420);
    rectangle(320,380,380,420);
    rectangle(400,380,469,420);

    gotoxy(37,11);
    cout<<"CALCULATOR";
    gotoxy(27,14);
    printf("1");
    gotoxy(36,14);
    printf("2");
    gotoxy(45,14);
    printf("3");
    gotoxy(55,14);
    printf("+");

    gotoxy(27,18);
    printf("4");
    gotoxy(36,18);
    printf("5");
    gotoxy(45,18);
    printf("6");
    gotoxy(55,18);
    printf("-");

    gotoxy(27,22);
    printf("7");
    gotoxy(36,22);
    printf("8");
    gotoxy(45,22);
    printf("9");
    gotoxy(55,22);
    printf("*");

    gotoxy(32,25);
    printf("0");
    gotoxy(45,25);
    printf("/");
    gotoxy(55,25);
    printf("=");

    //yash alekar//
    do
    {
    gotoxy(24,6);
    cout<<" ";
    gotoxy(24,7);
    cout<<" ";
    gotoxy(24,8);
    cout<<" ";
    gotoxy(24,6);
    float a;
    double b,c;
    int o;
    cout<<"enter 1st no//enter radius ";
    gotoxy(52,6);
    cin>>a;
    cout<<"addition=1"<>o;
    if(o==1)
    {
    gotoxy(24,8);
    cout<<"enter 2nd no ";
    gotoxy(52,8);
    cin>>b;
    c=a+b;
    gotoxy(24,8);
    cout<<"the addition is ";
    gotoxy(52,8);
    cout<>b;
    c=a-b;
    gotoxy(24,8);
    cout<<"the subtraction is ";
    gotoxy(52,8);
    cout<>b;
    c=a*b;
    gotoxy(24,8);
    cout<<"the multiplication is ";
    gotoxy(52,8);
    cout<>y;
    z=a/y;
    gotoxy(24,8);
    cout<<"the divison is ";
    gotoxy(52,8);
    cout<>s;
    }while(s==1);
    //yash alekar//
    clrscr();
    gotoxy(30,10);
    cout<<" -:created by:- ";
    gotoxy(33,12);
    cout<<"YASH ALEKAR";

    getch();
    }

    ReplyDelete
  5. its a good idea for me making of a calculator

    ReplyDelete
  6. Calculator program in C

    In C language we can design a program to add, subtract, multiply, divide any number, these all operation you can perform by using switch case.

    ReplyDelete
  7. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  8. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from

    ReplyDelete
  9. This blog awesome and i learn a lot about programming from here.The best thing about this blog is that you doing from beginning to experts level.

    Love from Pprogramming

    ReplyDelete