Calculator using 7 Different ways in C++

In this Article I have Provided Code of Calculator in C++ using 7 Different Methods

  1. using Switch case
  2. using if else
  3. using functions
  4. using recursive functions
  5. using classes
  6. using friend functions
  7. using operator overloading

Code for Calculator using Simple Switch Case in C++

//Calculator using switch statement
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
	float n1,n2,res;
	int ch;
	cout<<"\n\t================================================================\n"<<endl;
		cout<<"\n\t            Calculator using Switch "<<endl;
		cout<<"\n\t================================================================\n"<<endl;
	do
	{
	cout<<"Enter Two Numbers \n\n";
	cout<<"Number 1 : ";cin>>n1;
	cout<<"Number 2 : ";cin>>n2;
	cout<<"\nEnter choice what you want to calculate\n\n";
	cout<<"1-Addition(+)\n";
	cout<<"2-Subtract(-)\n";
	cout<<"3-Multiply(*)\n";
	cout<<"4-Divide(/)\n";
	cout<<"5-Modulus(%)\n";
	cout<<"6-Exit\n";
	cin>>ch;
	switch(ch)
	{
		case 1:
			cout<<n1<<" + "<<n2<<" = "<<n1+n2<<endl;
			break;
		case 2:
			cout<<n1<<" - "<<n2<<" = "<<n1-n2<<endl;
			break;
		case 3:
			cout<<n1<<" * "<<n2<<" = "<<n1*n2<<endl;
			break;
		case 4:
			cout<<n1<<" / "<<n2<<" = "<<n1/n2<<endl;
			break;
		case 5:
			cout<<(int)n1<<" % "<<(int)n2<<" = "<<(int)n1%(int)n2<<endl;
			break;
		default:
			cout<<"Thank you for using this :) \n";
			getch();
	}
    }
	while(ch>0 && ch<6);
}

Calculator using If Else in C++

//Calculator using if else statement
#include<iostream>
using namespace std;
#include<conio.h>
#include<math.h>
int main()
{
    float n1,n2;
    int ch;

    cout<<"\nEnter choice what you want to calculate\n\n";
    cout<<"1-Sum "<<endl;
    cout<<"2-Subtract "<<endl;
    cout<<"3-Multiply "<<endl;
    cout<<"4-divide"<<endl;
	cout<<"5-Modulus"<<endl;
	cout<<"6-Square"<<endl;
	cout<<"7-square Root "<<endl;
	cin>>ch;
	if(ch>=1 && ch<=5)
	{
	 cout<<"Enter Two Numbers \n";
    cin>>n1;
    cin>>n2;
	}
	else if(ch==6 || ch==7)
	{
		cout<<"Enter a number "<<endl;
		cin>>n1;
	}
    if(ch==1)
            cout<<n1<<" + "<<n2<<" = "<<n1+n2<<endl;
    else if(ch==2)
            cout<<n1<<" - "<<n2<<" = "<<n1-n2<<endl;
    else if(ch==3)
            cout<<n1<<" * "<<n2<<" = "<<n1*n2<<endl;
    else if(ch==4)
            cout<<n1<<" / "<<n2<<" = "<<n1/n2<<endl;
    else if(ch==5)
            cout<<n1<<" % "<<n2<<" = "<<(int)n1%(int)n2<<endl;
    else if(ch==6)
        	cout<<"Square of "<<n1<<" is "<<n1*n1<<endl;
    else if(ch==7)
        	cout<<"Square Root of "<<n1<<" is "<<sqrt(n1)<<endl;
    else
            cout<<"Thank you for using this  \n";

}

Calculator using functions in C++

//calculator using functions
#include<iostream>
#include<conio.h>
using namespace std;
float sum(float a,float b)
{
	return a+b;
}
float sub(float a,float b)
{
	return a-b;
}
float mul(float a,float b)
{
	return a*b;
}
float div(float a,float b)
{
	return a/b;
}
int mod(float a,float b)
{
	return (int)a%(int)b;
}
int main()
{
	cout<<"\n\t================================================================\n"<<endl;
		cout<<"\n\t            Calculator using Functions"<<endl;
		cout<<"\n\t================================================================\n"<<endl;
	float n1,n2,res;
	int ch;
	do
	{
	cout<<"Enter Two Numbers \n\n";
	cout<<"Number 1 : ";cin>>n1;
	cout<<"Number 2 : ";cin>>n2;
	cout<<"\nEnter choice what you want to calculate\n\n";
	cout<<"1-Addition(+)\n";
	cout<<"2-Subtract(-)\n";
	cout<<"3-Multiply(*)\n";
	cout<<"4-Divide(/)\n";
	cout<<"5-Modulus(%)\n";
	cout<<"6-Exit\n";
	cin>>ch;
	switch(ch)
	{
		case 1:
			cout<<n1<<" + "<<n2<<" = "<<sum(n1,n2)<<endl;
			break;
		case 2:
			cout<<n1<<" - "<<n2<<" = "<<sub(n1,n2)<<endl;
			break;
		case 3:
			cout<<n1<<" * "<<n2<<" = "<<mul(n1,n2)<<endl;
			break;
		case 4:
			cout<<n1<<" / "<<n2<<" = "<<div(n1,n2)<<endl;
			break;
		case 5:
			cout<<(int)n1<<" % "<<(int)n2<<" = "<<mod(n1,n2)<<endl;
			break;
		default:
			cout<<"Thank you for using this :) \n";
			getch();
	}
    }
	while(ch>0 && ch<6);
}

Calculator using Recursive functions in C++

#include<iostream>
#include<conio.h>
using namespace std;
int sum(int a,int b)
{
if(b==0)
	return a;
	else
	return (a,b-1)+1;
}
int sub(int a,int b)
{
	if(b==0)
	return a;
	if(a<0)
	return 1+(a,b-1);
	else if(b<0)
	return 1+(a,b+1);
	else
	return 1-(a,b-1);
}
int mul(int a,int b)
{
	if(b==0)
	return a;
	else
	return a+(a,b-1);
}
int div(int a,int b)
{
	if(b>=0)
	return 0;
	if(a<0)
	return 1+(a+b,b);
	else
	return 1+(a-b,b);
}
int mod(int a,int b)
{
		if(b>=0)
	return b;
	if(a<0)
	return 1+(a+b,b);
	else
	return (a-b,b);
}
int main()
{
	cout<<"\n\t================================================================\n"<<endl;
		cout<<"\n\t            Calculator using Recursion"<<endl;
		cout<<"\n\t================================================================\n"<<endl;
	float n1,n2,res;
	int ch;
	do
	{
	cout<<"Enter Two Numbers \n\n";
	cout<<"Number 1 : ";cin>>n1;
	cout<<"Number 2 : ";cin>>n2;
	cout<<"\nEnter choice what you want to calculate\n\n";
	cout<<"1-Addition(+)\n";
	cout<<"2-Subtract(-)\n";
	cout<<"3-Multiply(*)\n";
	cout<<"4-Divide(/)\n";
	cout<<"5-Modulus(%)\n";
	cout<<"6-Exit\n";
	cin>>ch;
	switch(ch)
	{
		case 1:
			cout<<n1<<" + "<<n2<<" = "<<sum(n1,n2)<<endl;
			break;
		case 2:
			cout<<n1<<" - "<<n2<<" = "<<sub(n1,n2)<<endl;
			break;
		case 3:
			cout<<n1<<" * "<<n2<<" = "<<mul(n1,n2)<<endl;
			break;
		case 4:
			cout<<n1<<" / "<<n2<<" = "<<div(n1,n2)<<endl;
			break;
		case 5:
			cout<<(int)n1<<" % "<<(int)n2<<" = "<<mod(n1,n2)<<endl;
			break;
		default:
			cout<<"Thank you for using this :) \n";
			getch();
	}
    }
	while(ch>0 && ch<6);
}

Calculator using Classes in C++

//Calculator using structure
#include<iostream>
using namespace std;

struct calculator
{
    float n1,n2,res;
		void input()
		{
			cout<<"\n";
			cout<<"\n\t";
			cout<<"Enter Number 1:\t";
			cin>>n1;
			cout<<"\tEnter Number 2:\t";
			cin>>n2;

		}
void sum()
{
	res=n1+n2;

}
void sub()
{
	res=n1-n2;
}
void mul()
{
	res=n1*n2;
}
void div()
{
	res=n1/n2;
}

	void print()
	{
		cout<<"\n\tAnswer is "<<res;
	}
};
int main()
{
		cout<<"\n\t================================================================\n"<<endl;
		cout<<"\n\t            Calculator using Structure"<<endl;
		cout<<"\n\t================================================================\n"<<endl;
	calculator n1;
	char op;
	do
	{
	n1.input();

	cout<<"\n\tEnter Operation\n";
			cout<<"\n\t-->Addition (+)\n";
			cout<<"\n\t-->Subtraction (-)\n";
			cout<<"\n\t-->Multiplication (*)\n";
			cout<<"\n\t-->Division (/)\n";
			cout<<"\n\t-->Exit(0)\n\n\t";
			cin>>op;
	switch (op)
	{
		case '+':
		n1.sum();
			n1.print();
			break;
		case '-':
			n1.sub();
			n1.print();
			break;
		case '*':
		n1.mul();
			n1.print();
			break;
		case '/':
		n1.div();
			n1.print();
			break;
		default:
			cout<<"\n\tTHANK YOU "<<endl;
			break;
	}

}while(op =='+' || op=='-'|| op=='*'||op=='/');
}

Calculator using Friend Functions in C++

#include<iostream>
using namespace std;
class num1;
class num2;
class calculator;
class num2
{
	friend class calculator;
	protected:
	int b;
	public:
		num2()
		{
			cout<<"\n\n";
			cout<<"\t";
			cout<<"Enter Number 2:\t";
			cin>>b;
		}
		calculator friend operator +(num2);
		calculator friend operator -(num2);
		calculator friend operator *(num2);
	//	calculator friend operator /(num2);
};
class num1
{
	friend class calculator;
	protected:
	int a;
	public:
		num1()
		{
			cout<<"\n\n";
			cout<<"\t";
			cout<<"Enter Number 1:\t";
			cin>>a;
		}
		calculator friend operator +(num2);
		calculator friend operator -(num2);
		calculator friend operator *(num2);
		//calculator friend operator /(num2);
};
class calculator
{
	int res;
	public:
		calculator()
		{
		}
		friend class num1;
		friend class num2;
	calculator friend operator +(num2 n2)
	{
		cout<<"\tAddition: "<<n1.a<<" + "<<n2.b<<" = "<<n1.a+n2.b<<endl;
	}
	calculator friend operator -(num2 n2)
	{
		cout<<"\tSubtraction: "<<n1.a<<" - "<<n2.b<<" = "<<n1.a-n2.b<<endl;
	}
	calculator friend operator *(num2 n2)
	{
		cout<<"\tMultiplication: "<<n1.a<<" * "<<n2.b<<" = "<<n1.a*n2.b<<endl;
	}
	calculator friend operator /(num2 n2)
	{
		cout<<"\tDivision: "<<n1.a<<" / "<<n2.b<<" = "<<n1.a/n2.b<<endl;
	}
};
int main()
{
		cout<<"\n\t================================================================\n"<<endl;
		cout<<"\n\t   Calculator using operator Overloading and Friend function"<<endl;
		cout<<"\n\t================================================================\n"<<endl;
	num1 n1;
	num2 n2;
	calculator c1;
	char op;
	cout<<"\n\tEnter Operation\n";
			cout<<"\n\tAddition (+)\n";
			cout<<"\n\tSubtraction (-)\n";
			cout<<"\n\tMultiplication (*)\n";
			cout<<"\n\tDivision (/)\n";
			cout<<"\n\t Exit (press any other key)\n\t";
			cin>>op;
	switch (op)
	{
		case '+':
			n1+n2;
			break;
		case '-':
			n1-n2;
			break;
		case '*':
			n1*n2;
			break;
		case '/';
			n1/n2;
			break;
		default:
			cout<<"\n\tTHANK YOU "<<endl;
			break;
	}


}

Calculator using Operator Overloading in C++

//calculator using classes and operator overloading
#include<iostream>
using namespace std;

class calculator
{
    int a;
	public:
		calculator()
		{
			cout<<"\n";
			cout<<"\n\t";
			cout<<"Enter Number :\t";
			cin>>a;

		}
		calculator(int n)
		{
			a=n;
		}
	calculator  operator +(calculator n2)
	{
		calculator c(0);
		c.a=a+n2.a;
		return c;
	}
	calculator operator -(calculator n2)
	{
		calculator c(0);
		c.a=a-n2.a;
		return c;
	}
	calculator operator *(calculator n2)
	{
		calculator c(0);
		c.a=a*n2.a;
		return c;
	}
	calculator  operator /(calculator n2)
	{
		calculator c(0);
		c.a=a/n2.a;
		return c;
	}
	void print()
	{
		cout<<"\n\tAnswer is "<<a;
	}
};
int main()
{
		cout<<"\n\t================================================================\n"<<endl;
		cout<<"\n\t            Calculator using operator overloading"<<endl;
		cout<<"\n\t================================================================\n"<<endl;
	calculator n1,n2,n3(0);
	char op;
	cout<<"\n\tEnter Operation\n";
			cout<<"\n\tAddition (+)\n";
			cout<<"\n\tSubtraction (-)\n";
			cout<<"\n\tMultiplication (*)\n";
			cout<<"\n\tDivision (/)\n\n\t";
			cin>>op;
	switch (op)
	{
		case '+':
			n3=n1+n2;
			n3.print();
			break;
		case '-':
			n3=n1-n2;
			n3.print();
			break;
		case '*':
			n3=n1*n2;
			n3.print();
			break;
		case '/':
			n3=n1/n2;
			n3.print();
			break;
		default:
			cout<<"\n\tTHANK YOU "<<endl;
			break;
	}


}

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *