Saturday, October 17, 2015

Well explained Program to make a calculator C++

In this post we will create a simple calculator in our program today. The user will enter the first value then he will give the arithmetic operator and then he will give the second value. The compiler will then give the answer against the arithmetic operators, in this program we will learn little bit about TEXT data types.

The complete code of this program is as follows:

#include <iostream>
using namespace std;
int main (void)
{
         int a, b,c ;
         char op;
         
         
         cout << "Enetr the value" <<"\n";  

        cin >> a ;
        cout << "Enter the other value" <<"\n";
        cin >> b; 
        cout << "Enter arethmetic operation" <<"\n";
        cin >> op;
        if (op == '+')
        {cout << a + b;
          }
          
          if (op == '-')
         { cout << a - b;
          }
          
          
          if ( op == '/')
         { cout << a / b;
          }
          
          if ( op == '*')
          {cout << a * b;
          }
          
                      
       system ("pause");
       return 0;
    
 
}

#include <iostream>
using namespace std;
int main (void)
{
         int a, b,c ;
         char op;

First of all we assign 3 variables of int data type a,b and c. The values of variables will be taken by the user through cin data stream. Now we will assign a text data type which will use characters and is
named as char text data type. Basically text data type contains only texts and characters. There are two types of data types, char data type which only contain characters i.e “+, -, /, *”. Basically char data type store 1 byte of memory. The second text data type is string data type, it contains words, names characters as well. We will study in detail about string data type in our coming programs. Here we are using char data type and the variable we are using is op as a short form of operator.
            
         cout << "Enter the value" <<"\n";

Now when the program executes it will display the output as written in cout stream data.

        cin >> a ;

Here the cin will collect the first value from the user which will store in the variable a.

        cout << "Enter the other value" <<"\n";

This cout statement will display on the screen after which the user will enter the second value

        cin >> b; 

The user will enter second value, which will store in variable b.

        cout << "Enter arithmetic operation" <<"\n";
        cin >> op;

Now the user will enter the operator through cin stream data, as describe above the user would be able to enter character as the data type is character.

Now to execute the proper result we give the program conditions, we are using four conditions for four basic arithmetic operations. We are using here” if” statement for every condition. Every time the condition will execute the output on the base of operator. The program will only execute in such condition where relational expression becomes true. If the relational condition becomes true then the conditional statement which is written in the cout stream data will perform task and execute the output. Which condition becomes true it will execute and the other three conditions then will skip, because in  “if” condition the compiler only execute the relational expression when it is true.

        if (op == '+')

Here in the first “if” condition if the operator is equal to “+” then the compiler will execute the conditional statement which is written in cout stream data inside the body of “if” statement. In the “if” statements expression the “op” is variable of char data type which was given by the user. The double is equal to “==” sign is called relational operator, here it state that if the operator is equal to “+”  then the “if” condition will become true otherwise it will be skip and will move to other condition. Basically relational operators are used for comparison.

        {cout << a + b;

If the relational expression of “if” condition becomes true then the task given by the cout stream data will perform and the values of variable “a” and ”b” will add.

          }       
          if (op == '-')
         { 
              cout << a - b;
          }             
In case when users wants to subtract and presses '-'. The program will display the difference of two entered numbers.

          if ( op == '/')
         { 
               cout << a / b;
          }       
In case when users wants to divide and presses '/'. The program will display the quotient of two entered numbers.

          if ( op == '*')
          {
              cout << a * b;
          }
In case when users wants to multiply and presses '*'. The program will display the product of two entered numbers.
                             
       system ("pause");
       return 0;
}

In the end we just wait till users presses a key to end the program by returning 0.

 




No comments:

Post a Comment