Calculator program in C++


Calculator program in C++ using function and do-while loop:-







Requirement :- Turbo C++  download

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. }


Output:-


Using do-while Loop

Write a Calculator Program in the C++ using the do-while  and Switch Statement.

  1. #include<iostream.h>  
  2. #include<stdio.h>  
  3. #include<conio.h>  
  4. #include<stdlib.h>  
  5. void main()  
  6. {  
  7. clrscr();  
  8. int opr;  
  9. int num1, num2, x;  
  10. // display different operation of the calculator  
  11. do  
  12. {  
  13. cout << "Select an operation to perform a simple calculation in C++ Calculator"  
  14.      "\n1 = Addition"  
  15.      "\n2 = Subtraction"  
  16.      "\n3 = Multiplication"  
  17.      "\n4 = Division"  
  18.      "\n5 = Square"  
  19.      "\n6 = Exit"  
  20.      "\n \n Make a choice: ";  
  21.      cin >> opr;  
  22.    switch (opr)  
  23.      {  
  24.      // for addition operation in calculator  
  25.      case 1:  
  26.      cout << "You have selected the Addition Operation.";  
  27.      cout << "\n Please enter the two number: \n";  
  28.      cin >> num1 >> num2;  
  29.      x = num1 + num2;  
  30.      cout << "Sum of two number = " << x;  
  31.      break;  
  32.      // for subtraction operation in calculator  
  33.      case 2:  
  34.      cout << "You have selected the Subtraction Operation.";  
  35.      cout << "\n Please enter the two number: \n";  
  36.      cin >> num1 >> num2;  
  37.      x = num1 - num2;  
  38.      cout << "Subtraction of two number = " << x;  
  39.      break;  
  40.      // for multiplication operation in calculator  
  41.      case 3:  
  42.      cout << "You have selected the Multiplication Operation.";  
  43.      cout << "\n Please enter the two number: \n";  
  44.      cin >> num1 >> num2;  
  45.      x = num1 * num2;  
  46.      cout << "Product of two number = " << x;  
  47.      break;  
  48.      // for division operation in calculator  
  49.      case 4:  
  50.      cout << "You have selected the Division Operation.";  
  51.      cout << "\n Please enter the two number; \n";  
  52.      cin >> num1 >> num2;  
  53.      // while loop checks for divisor whether it is zero  
  54.      while ( num2 == 0)  
  55.      {  
  56.      cout << "\n Divisor cannot be zero"  
  57.          "\n Please enter the divisor once again: ";  
  58.          cin >> num2;  
  59.          }  
  60.      x = num1 / num2;  
  61.      cout << "\n Quotient = " << x;  
  62.      break;  
  63.      // to square a number in calculator  
  64.      case 5:  
  65.      cout << "You have selected the Square Operation.";  
  66.       cout << "\n Please enter any number: \n";  
  67.      cin >> num1;  
  68.      x = num1 * num1;  
  69.      cout << "Square is = " << x;  
  70.      break;  
  71.      case 6: exit(0);  // terminate the program  
  72.      break;  
  73.      default: cout << "\n Something went wrong..!!";  
  74.      break;  
  75.      }  
  76.      cout << "\n----------------------------------------- \n";  
  77.      } while(opr != 6);  
  78.      getch();  
  79.      }  

Output:-


                                                        and its  done !! 




































































                             

Comments

Popular Posts