-
Notifications
You must be signed in to change notification settings - Fork 10
/
calculator.cpp
131 lines (123 loc) · 2.79 KB
/
calculator.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <bits/stdc++.h>
#include <stdio.h>
using namespace std;
void add();
void sub();
void multiply();
void division();
void Square();
void SquareRoot();
void exit();
int main()
{
int Operator;
do
{
cout<<"Select an operation from the Calculator"
"\nPress 1 for Addition"
"\nPress 2 for Subtraction"
"\nPress 3 for Multiplication"
"\nPress 4 for Division"
"\nPress 5 for Square"
"\nPress 6 for Square Root"
"\nPress 7 to Exit"
"\n \n Make a choice: ";
cin >> Operator;
switch (Operator)
{
case 1:
add(); // call add() function to find the Addition
break;
case 2:
sub(); // call sub() function to find the subtraction
break;
case 3:
multiply(); // call multiply() function to find the multiplication
break;
case 4:
division(); // call division() function to find the division
break;
case 5:
Square(); // call Square() function to find the square of a number
break;
case 6:
SquareRoot(); // call SquareRoot() function to find the Square Root of the given number
break;
case 7:
exit(0); // terminate the program
break;
default:
cout <<"Something is wrong..!!";
break;
}
cout <<" \n------------------------------\n";
}while(Operator != 7);
return 0;
}
void add()
{
int n, sum = 0, i, number;
cout <<"How many numbers you want to add: ";
cin >> n;
cout << "Please enter the number one by one: \n";
for (i = 1; i <= n; i++)
{
cin >> number;
sum = sum + number;
}
cout << "\n Sum of the numbers = "<< sum;
}
void sub()
{
int num1, num2, z;
cout <<" \n Enter the First number = ";
cin >> num1;
cout << "\n Enter the Second number = ";
cin >> num2;
z = num1 - num2;
cout <<"\n Subtraction of the number = " << z;
}
void multiply()
{
int num1, num2, mul;
cout <<" \n Enter the First number = ";
cin >> num1;
cout << "\n Enter the Second number = ";
cin >> num2;
mul = num1 * num2;
cout <<"\n Multiplication of two numbers = " << mul;
}
void division()
{
int num1, num2, div = 0;
cout <<" \n Enter the First number = ";
cin >> num1;
cout << "\n Enter the Second number = ";
cin >> num2;
while ( num2 == 0)
{
cout << "\n Divisor cannot be zero"
"\n Please enter the divisor once again: ";
cin >> num2;
}
div = num1 / num2;
cout <<"\n Division of two numbers = " << div;
}
void Square()
{
int num1;
float sq;
cout <<" \n Enter a number to find the Square: ";
cin >> num1;
sq = num1 * num1;
cout <<" \n Square of " << num1<< " is : "<< sq;
}
void SquareRoot()
{
float n;
int num;
cout << "\n Enter the number to find the Square Root:";
cin >> num;
n = sqrt(num);
cout <<" \n Square Root of " << num<< " is : "<< n;
}