0% found this document useful (0 votes)
6 views

Calculator Assignment by Afaq 23204

Uploaded by

umermunir01.pk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Calculator Assignment by Afaq 23204

Uploaded by

umermunir01.pk
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Done by Afaq Ahmad Yar (23204)

Q. Write a program to create a calculator by


using function method.
Solution :
#include <iostream>

#include <cmath>

using namespace std;

double sum(double x, double y)

cout << x << " + " << y << " = " ;

return x+y;

double sub(double x, double y)

cout << x << " - " << y << " = " ;

return x-y;

double mul(double x, double y)

cout << x << " * " << y << " = " ;

return x*y;

double divi(double x, double y)

cout << x << " / " << y << " = " ;

return x/y;

int mod(int x, int y)


{

cout << x << " % " << y << " = " ;

return x%y;

int main()

cout << "//***" << endl;

cout << " " << "('+' for sum)" << endl;

cout << " " << "('-' for subtraction)" << endl;

cout << " " << "('*' for multiplication)" << endl;

cout << " " << "('/' for division)" << endl;

cout << " " << "('%' for modolus)" << endl;

cout << " " << "('2' for square)" << endl;

cout << " " << "('3' for cube)" << endl;

cout << " " << "('p or P' for exponentiation)" << endl;

cout << " " << "('s or S' for square root)" << endl;

cout << " " << "('c or C' for cube root root)" << endl;

cout << "***/// "<< endl;

double x;

char ch;

do {

cout << "Enter a number :" << endl;

cin >> x;

cout << "Enter an operator or specified charactor to perform different operations :" << endl;

cin >> ch;

if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%')

double y;

cout << "Enter another number :" << endl;

cin >> y;

if (ch == '+')

cout << sum(x,y);


}

else if (ch == '-')

cout << sub(x,y);

else if (ch == '*')

cout << mul(x,y);

else if (ch == '/')

if (y != 0) cout << divi(x,y);

else cout << "Error, denominator cannot be equal to zero." ;

else

cout << mod(x,y);

else if (ch == 's' || ch == 'S' || ch == 'c' || ch == 'C' || ch == '2' || ch == '3')

if (ch == 's' || ch == 'S')

cout << "Square root is " << sqrt(x);

else if (ch == 'c' || ch == 'C')

cout << "cube root is " << cbrt(x);

else if (ch == '2')

cout << "square is " << pow(x,2);

}
else

cout << "cube is " << pow(x,3);

else if (ch == 'p' || ch == 'P')

double y;

cout << "Power of " ;

cin >> y;

cout << x << " power of " << y << " = " << pow(x,y);

else

cout << "Enter operators as mentioned above..." << endl ;

cout <<endl<< "Do you want to calculate further, type 'y' for yes or 'n' for no :" << endl;

cin >> ch;

cout << "__________________________" << endl;

while (ch == 'y' || ch == 'Y') ;

cout << "Goob by. Have a nice day." << endl;

return 0;

OUTPUT
//***
('+' for sum)
('-' for subtraction)
('*' for multiplication)
('/' for division)
('%' for modolus)
('2' for square)
('3' for cube)
('p or P' for exponentiation)
('s or S' for square root)
('c or C' for cube root root)
***///
Enter a number :
9
Enter an operator or specified charactor to perform different
operations :
+
Enter another number :
98
9 + 98 = 107
Do you want to calculate further, type 'y' for yes or 'n' for
no :
y
__________________________
Enter a number :
9
Enter an operator or specified charactor to perform different
operations :
3
cube is 729
Do you want to calculate further, type 'y' for yes or 'n' for
no :
y
__________________________
Enter a number :
6
Enter an operator or specified charactor to perform different
operations :
p
Power of 9
6 power of 9 = 1.00777e+007
Do you want to calculate further, type 'y' for yes or 'n' for
no :
y
__________________________
Enter a number :
27
Enter an operator or specified charactor to perform different
operations :
c
cube root is 3
Do you want to calculate further, type 'y' for yes or 'n' for
no :
n
__________________________
Goob by. Have a nice day.

You might also like