codescracker


c++

C++ Program to Make Simple Calculator



« Previous Program Next Program »


Make Simple Calculator Program in C++

To make a simple calculator in C++ programming which performs basic four mathematical operations (addition, subtraction, multiplicatin, and division) depending on the user's choice, use the switch case to identify the input operator to perform required calculation then display the result as shown here in the following program.

C++ Programming Code to Make Simple Calculator

Following is a simple C++ program which is a menu-driven program based on simple calculation like addition, subtraction, multiplication and division according to user's choice:

/* C++ Program - Make Simple Calculator */
		
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
	clrscr();
	float a, b, res;
	char choice, ch;
	do
	{
		cout<<"1.Addition\n";
		cout<<"2.Subtraction\n";
		cout<<"3.Multiplication\n";
		cout<<"4.Division\n";
		cout<<"5.Exit\n\n";
		cout<<"Enter Your Choice : ";
		cin>>choice;
		switch(choice)
		{
			case '1' : cout<<"Enter two number : ";
				cin>>a>>b;
				res=a+b;
				cout<<"Result = "<<res;
				break;
			case '2' : cout<<"Enter two number : ";
				cin>>a>>b;
				res=a-b;
				cout<<"Result = "<<res;
				break;
			case '3' : cout<<"Enter two number : ";
				cin>>a>>b;
				res=a*b;
				cout<<"Result = "<<res;
				break;
			case '4' : cout<<"Enter two number : ";
				cin>>a>>b;
				res=a/b;
				cout<<"Result = "<<res;
				break;
			case '5' : exit(0);
				break;
			default : cout<<"Wrong Choice..!!";
				break;
		}
		cout<<"\n------------------------------------\n";
	}while(choice!=5 && choice!=getchar());
	getch();
}

When the above C++ program is compile and executed, it will produce the following result:

C++ program to make simple calculator

Same Program in Other Language

You may like the same program in other programming languages:


« Previous Program Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test