Pages

Wednesday, August 14, 2013

Write a calculator program in C++

This program add and subtract two integers read from the keyboard

Source Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    #include <iostream.h>
    #include <conio.h>
    #include <iomanip>
    using namespace std;
    // Prototype Declaration 
    int add  (int a, int b);
    int subt (int a, int b);
    int main ()
    {
        // Prompt user for input and get data
    cout << "\n Enter two integer numbers: ";
    // Read numbers into a and b
    int a, b;
    cin  >> a >> b ;
    // Calculate the sum and difference
    int sum  = add  (a, b);
    int diff = subt (a, b);
    cout << setw(5) << a << "  +  " << b << "  =  " <<  sum  << endl;
    cout << setw(5) << a << "  -  " << b << "  =  " <<  diff << endl;
    // Close Program
    getch();
    return 0;
           } // main
    /* =============== add =====================
       This function adds two integer and returns the sum
       Pre  Parameter a and b
       Post Returns a + b            */  
    int add (int a, int b)
    {
     return (a + b);
                    } // add   
    /* =============== subt =====================
       Returns the difference of two integer
       Pre  Parameter a and b
       Post Returns a - b            */  
    int subt (int a, int b)
    {
     return (a - b);       
                    } // subt

Result:


 Enter two integer numbers: 5  10
    5  +  10  =  15
    5  -  10  =  -5

No comments:

Post a Comment