A calculator is a portable device that helps to
perform simple mathematical calculations in our daily lives such as addition,
subtraction, division, multiplication, etc. In this section, we will
create calculator program in C++ using function.
Using Function
Lets' create a calculator program in C++ using
the function and Switch statement.
1. #include<iostream.h>
2. #include<stdio.h>
3. #include<conio.h>
4. #include<math.h>
5. #include<stdlib.h>
6. void add();
7. void sub();
8. void multi();
9. void division();
10. void sqr();
11. void srt();
12. void exit();
13. void main()
14. {
15. clrscr();
16. int opr;
17. // display different operation of the calculator
18. do
19. {
20. cout << "Select any operation from the C++ Calculator"
21. "\n1 = Addition"
22. "\n2 = Subtraction"
23. "\n3 = Multiplication"
24. "\n4 = Division"
25. "\n5 = Square"
26. "\n6 = Square Root"
27. "\n7 = Exit"
28. "\n \n Make a choice: ";
29. cin >> opr;
30.
31. switch (opr)
32. {
33. case 1:
34. add(); // call add() function to find the Addition
35. break;
36. case 2:
37. sub(); // call sub() function to find the subtraction
38. break;
39. case 3:
40. multi(); // call multi() function to find the multiplication
41. break;
42. case 4:
43. division(); // call division() function to find the division
44. break;
45. case 5:
46. sqr(); // call sqr() function to find the square of a number
47. break;
48. case 6:
49. srt(); // call srt() function to find the Square Root of the given number
50. break;
51. case 7:
52. exit(0); // terminate the program
53. break;
54. default:
55. cout <<"Something is wrong..!!";
56. break;
57. }
58. cout <<" \n------------------------------\n";
59. }while(opr != 7);
60. getch();
61. }
62.
63. void add()
64. {
65. int n, sum = 0, i, number;
66. cout <<"How many numbers you want to add: ";
67. cin >> n;
68. cout << "Please enter the number one by one: \n";
69. for (i = 1; i <= n; i++)
70. {
71. cin >> number;
72. sum = sum + number;
73. }
74. cout << "\n Sum of the numbers = "<< sum;
75. }
76. void sub()
77. {
78. int num1, num2, z;
79. cout <<" \n Enter the First number = ";
80. cin >> num1;
81. cout << "\n Enter the Second number = ";
82. cin >> num2;
83. z = num1 - num2;
84. cout <<"\n Subtraction of the number = " << z;
85. }
86. void multi()
87. {
88. int num1, num2, mul;
89. cout <<" \n Enter the First number = ";
90. cin >> num1;
91. cout << "\n Enter the Second number = ";
92. cin >> num2;
93. mul = num1 * num2;
94. cout <<"\n Multiplication of two numbers = " << mul;
95. }
96. void division()
97. {
98. int num1, num2, div = 0;
99. cout <<" \n Enter the First number = ";
100.
cin >> num1;
101.
cout << "\n Enter the Second number = ";
102.
cin >> num2;
103.
while ( num2 == 0)
104.
{
105.
cout << "\n Divisor canot be zero"
106.
"\n Please enter the divisor once again: ";
107.
cin >> num2;
108.
}
109.
div = num1 / num2;
110.
cout <<"\n Division of two numbers = " << div;
111.
}
112.
void sqr()
113.
{
114.
int num1;
115.
float sq;
116.
cout <<" \n Enter a number to find the Square: ";
117.
cin >> num1;
118.
sq = num1 * num1;
119.
cout <<" \n Square of " << num1<< " is : "<< sq;
120.
}
121.
void srt()
122.
{
123.
float q;
124.
int num1;
125.
cout << "\n Enter the number to find the Square Root:";
126.
cin >> num1;
127.
q = sqrt(num1);
128.
cout <<" \n Square Root of " << num1<< " is : "<< q;
129.
}