-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.cpp
More file actions
111 lines (102 loc) · 3.67 KB
/
Calculator.cpp
File metadata and controls
111 lines (102 loc) · 3.67 KB
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
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
int32_t main()
{
int opr;
int num1, num2, x;
// display different operation of the calculator
do
{
cout << "Select an operation to perform a simple calculation in C++ Calculator"
"\n1 = Addition"
"\n2 = Subtraction"
"\n3 = Multiplication"
"\n4 = Division"
"\n5 = Factorial"
"\n6 = Square"
"\n7 = Square Root"
"\n8 = Exit"
"\n \n Make a choice: ";
cin >> opr;
switch (opr)
{
// for addition operation in calculator
case 1:
cout << "You have selected the Addition Operation.";
cout << "\n Please enter the two number: \n";
cin >> num1 >> num2;
x = num1 + num2;
cout << "Sum of two number " << num1 << " & " << num2 << " is: " << x;
break;
// for subtraction operation in calculator
case 2:
cout << "You have selected the Subtraction Operation.";
cout << "\n Please enter the two number: \n";
cin >> num1 >> num2;
x = num1 - num2;
cout << "Subtraction of two number " << num1 << " & " << num2 << " is: " << x;
break;
// for multiplication operation in calculator
case 3:
cout << "You have selected the Multiplication Operation.";
cout << "\n Please enter the two number: \n";
cin >> num1 >> num2;
x = num1 * num2;
cout << "Product of two number " << num1 << " & " << num2 << " is: " << x;
break;
// for division operation in calculator
case 4:
cout << "You have selected the Division Operation.";
cout << "\n Please enter the two number; \n";
cin >> num1 >> num2;
// while loop checks for divisor whether it is zero
while (num2 == 0)
{
cout << "\n Divisor cannot be zero"
"\n Please enter the divisor once again: ";
cin >> num2;
}
x = num1 / num2;
cout << "\n Quotient = " << x;
break;
// for factorial operation in calculator
case 5:
int num, fact;
cout << "You have selected the factorial Operation." << endl;
cout << "Enter a number: ";
cin >> num;
fact = 1;
for (int i = 1; i <= num; i++)
{
fact *= i;
}
cout << "The Factorial of " << num << " is: " << fact << endl;
break;
// to square a number in calculator
case 6:
cout << "You have selected the Square Operation.";
cout << "\n Please enter any number: \n";
cin >> num1;
x = num1 * num1;
cout << "Square of " << num1 << " is: " << x;
break;
case 7: // for square root operation in calculator
cout << "You have selected the Square Root Operation.";
cout << "\n Please enter any number: \n";
cin >> num;
x = sqrt(num); // use sqrt() function to find the Square Root
cout << " Square Root of " << num << " is: " << x << endl;
break; // break the function
case 8:
exit(0); // terminate the program
break;
default:
cout << "\n Something went wrong..!!";
break;
}
cout << "\n ***********************************\n";
} while (opr != 6);
}