0% found this document useful (0 votes)
14 views3 pages

Calculator Program

The document describes a C++ program that implements various mathematical functions like addition, subtraction, multiplication, division, square, cube, power, and factorial. The program uses functions to perform the calculations and a switch case to allow the user to select the operation.

Uploaded by

urwazahra01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views3 pages

Calculator Program

The document describes a C++ program that implements various mathematical functions like addition, subtraction, multiplication, division, square, cube, power, and factorial. The program uses functions to perform the calculations and a switch case to allow the user to select the operation.

Uploaded by

urwazahra01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include "stdafx.

h"
#include <iostream>
using namespace std;

float sum (float a, float b)


{
return a+b;}

float diff (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;}

float sqr(float a)
{ return a*a;
}

float cube(float a)
{ return a*a*a;
}

float pow(int n, int b)


{ int result=1;
if(b==0)
{return 1;}

else

return n*pow(n,b-1);
}

int factorial(int n)
{
int fact = 1;
for(int i = 1; i <= n; i++)
{
fact *= i;
}
return fact;
}

int main()
{

cout<<"\n\t-----------------------------------------\n";
cout<<"\n\t----------------CALCULATOR-------------\n";
cout<<"\n\t-----------------------------------------\n";
cout<<"\t1 addition"<<endl;
cout<<"\t2 subtraction"<<endl;
cout<<"\t3 multiplication"<<endl;
cout<<"\t4 division"<<endl;
cout<<"\t5 square"<<endl;
cout<<"\t6 cube"<<endl;
cout<<"\t7 power"<<endl;
cout<<"\t8 factorial"<<endl;
cout<<"\t9 exit"<<endl;
int z;
do{
cout<<"\n\tEnter the Function you want to perform:"<<endl;
cin>>z;
switch (z)
{
case 1:
{float num1,num2;
cout<<"enter number 1"<<endl;
cin>>num1;
cout<<"enter number 2"<<endl;
cin>>num2;

cout<<sum(num1,num2);
break;}
case 2:
{ float num1,num2;
cout<<"enter number 1"<<endl;
cin>>num1;
cout<<"enter number 2"<<endl;
cin>>num2;

cout<<diff(num1,num2);
break;}

case 3:
{float num1,num2;
cout<<"enter number 1"<<endl;
cin>>num1;
cout<<"enter number 2"<<endl;
cin>>num2;
cout<<mul(num1,num2);
break;}

case 4:
{float num1,num2;
cout<<"enter number 1"<<endl;
cin>>num1;
cout<<"enter number 2"<<endl;
cin>>num2;
cout<<div(num1,num2);
break;}

case 5:
{float num1;
cout<<"enter number 1"<<endl;
cin>>num1;
cout<<sqr(num1);
break;}
case 6:
{ float num1;
cout<<"enter number 1"<<endl;
cin>>num1;
cout<<cube(num1);
break;

}
case 7:
{
float n,b;
cout<<"Enter the number"<<endl;
cin>>n;
cout<<"Enter the exponent"<<endl;
cin>>b;
float result=pow(n,b);
cout<<"power is"<<result<<endl;
break;}
case 8:
{
int n;
cout << "Enter a positive integer: ";
cin >> n;

if(n>=0)
{
cout << "Factorial of " << n << " = " << factorial(n) << endl;
}
else
{
cout << "Invalid input. Factorial of negative numbers is not defined." << endl;
}
break;
}
case 9:
break;

default:
cout<<"\n\tInvalid entry"<<endl ;
}}
while(z!=9);
system("pause");
return 0;
}

You might also like