Himanshu

Negi

Developer & Internet Marketer


Welcome to my blog.

Sed sed leo sit amet elit sagittis fermentum. Nulla posuere commodo erat. Nam ac nunc ipsum, id espara tincidunt orci. Quisque adipiscing molestie dolor, ut congue sem porta et. Sed sapien urna, auctor et accumsan ut, accumsan sit amet dui. Nunc tristique interdum magna, dictum volutpat sem. Nam ac nunc ipsum, id espara tincidunt orci. Sed sapien urna, auctor et accumsan ut.

How to Create a Simple C++ Calculator Program?

April 29, 2017wp-himu1 Comment

The simple C++ calculator is a program that performs arithmetic operations on numbers. The C++ calculator we are going to create is simple but can perform arithmatic operations like addition, subtraction, multiplication and division. Are you ready to Create a Simple C++ Calculator Program?

Actually, I find a lot of C++ calculator programs online but none of them were programmed so well so I decided to create a neat and simple C++ calculator program for all you and all beginners.

The C++ Calculator program asks the user to enter two numbers and then ask the user to tell the type of operation he/she wants to be performed. The arithmetic operation on C++ variables will be performed according to user’s choice. Let’s take a look at the C++ programming code.

[codesyntax lang=”cpp” title=”simple C++ calculator ” bookmarkname=”simple C++ calculator “]

#include<iostream.h>
#include<conio.h>

Void main()
{
        int number1;
        int number2;
        char choice;

        cout<<"Enter first number:";
        cin>>number1;
        cout<<"Enter second number";
        cin>>number2;

        cout<<"Enter your Choice (+ - / * ):";
        cin>>choice;

        switch(choice)
        {
                case ‘+’:          cout<<"Addition result="<<number1+number2;
               		           break;
                case ‘-‘:          cout<<"Subtraction result="<<number1-number2;
		                   break;
                case ‘/’:          cout<<"Division result="<<number1/number2;
		                   break;
                case ‘*’:          cout<<"Multiplication result="<<number1*number2;
	        	           break;

                default:            cout<<"Invalid choice!";
}

getch();
}

[/codesyntax]

 

c++ calculator

Simple Working of C++ Calculator:

[codesyntax lang=”cpp” title=”Header files included” bookmarkname=”Header files included”]

#include<iostream.h>   // for cout and cin 
#include<conio.h>          // for getch()

[/codesyntax]

Then as usual the execution starts from main() i.e. main function.

Then three variables are created
[codesyntax lang=”cpp” title=”variables used” bookmarkname=”variables used”]

int number1; 
int number2; 
char choice;

[/codesyntax]

First variable (number1) will be used to hold first value and number2 will hold the second value. The choice variable will hold the arithmetic operator (+,-,*,/). Then
[codesyntax lang=”cpp”]

cout<<”Enter first number:”; 
cin>>number1; 
cout<<”Enter second number:”; 
cin>>number2;

[/codesyntax]

These statements will ask the user to enter two numbers one by one and then stores it in respective variables.
[codesyntax lang=”cpp”]

cout<<”Enter your Choice(+-/*):”; 
cin>>choice;

[/codesyntax]

Now the user will prompted by program to decide the type of operation (or simply, calculation) he/she wants to perform. Then the user choice will be stored in choice variable. Now the switch case statement comes into action
[codesyntax lang=”cpp” title=”switch-case used in C++ calculator” bookmarkname=”switch-case used in C++ calculator”]

switch(choice) 
{
case ‘+’:             cout<<”Addition result=”<<number1+number2;
                         break;
: 
:
:
:

default:               cout<<”Invalid choice!”;
}

[/codesyntax]

This is the most important part.The value in the choice variable will be matched with each case one by one and the case matched will be qualified for execution and to stop further unnecessary case execution we use break keyword. Break keyword will stop execution inside that switch-case block and the execution starts from next line.
[codesyntax lang=”cpp”]

getch();

[/codesyntax]

Finally the getch function will hold the screen until a keyboard key is pressed. Oh, I forget to tell that calculated result will be displayed by respective case that is matched with switch.

As I mentioned this C++ calculator is very simple and do the basic arithmetic operations only. You can easily add more functionality to it by adding more cases (for example remainder functionality (% operator is used)). Also note that this C++ calculator only works with integer variables, you can make fractional operation possible by defining number1 and number2 variables as float data type but modular function doesn’t work with float variables. I hope you liked the simple C++ calculator.

This article has one Comment
  1. salmaaan
    March 14, 2013

    Bohat Bhaddiya !!


Leave a Reply
www.edatastyle.com www.introducingsomething.com