forked from imnurav/Hactoberfest2021-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculator.cpp
129 lines (127 loc) · 2.88 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
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void add();
void sub();
void multi();
void division();
void sqr();
void srt();
void exit();
void main()
{
clrscr();
int opr;
// display different operation of the calculator
do
{
cout << "Select any operation from the C++ Calculator"
"\n1 = Addition"
"\n2 = Subtraction"
"\n3 = Multiplication"
"\n4 = Division"
"\n5 = Square"
"\n6 = Square Root"
"\n7 = Exit"
"\n \n Make a choice: ";
cin >> opr;
switch (opr)
{
case 1:
add(); // call add() function to find the Addition
break;
case 2:
sub(); // call sub() function to find the subtraction
break;
case 3:
multi(); // call multi() function to find the multiplication
break;
case 4:
division(); // call division() function to find the division
break;
case 5:
sqr(); // call sqr() function to find the square of a number
break;
case 6:
srt(); // call srt() 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(opr != 7);
getch();
}
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 multi()
{
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 canot be zero"
"\n Please enter the divisor once again: ";
cin >> num2;
}
div = num1 / num2;
cout <<"\n Division of two numbers = " << div;
}
void sqr()
{
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 srt()
{
float q;
int num1;
cout << "\n Enter the number to find the Square Root:";
cin >> num1;
q = sqrt(num1);
cout <<" \n Square Root of " << num1<< " is : "<< q;
}