Skip to main content

Write a program to dispaly the different operation of calculator in C++

  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. }  
Anurag Rana Educator CSE/IT

Comments

Popular posts from this blog

Standard and Formatted Input / Output in C++

The C++ standard libraries provide an extensive set of input/output capabilities which we will see in subsequent chapters. This chapter will discuss very basic and most common I/O operations required for C++ programming. C++ I/O occurs in streams, which are sequences of bytes. If bytes flow from a device like a keyboard, a disk drive, or a network connection etc. to main memory, this is called   input operation   and if bytes flow from main memory to a device like a display screen, a printer, a disk drive, or a network connection, etc., this is called   output operation . Standard Input and Output in C++ is done through the use of  streams . Streams are generic places to send or receive data. In C++, I/O is done through classes and objects defined in the header file  <iostream> .  iostream  stands for standard input-output stream. This header file contains definitions to objects like  cin ,  cout , etc. /O Library Header Files There are...

Genetic Algorithm: Population, Fitness Function, Parent Selection, Cross over, Mutation

Genetic Algo Population Population is a subset of solutions in the current generation. It can also be defined as a set of chromosomes. There are several things to be kept in mind when dealing with GA population − The diversity of the population should be maintained otherwise it might lead to premature convergence. The population size should not be kept very large as it can cause a GA to slow down, while a smaller population might not be enough for a good mating pool. Therefore, an optimal population size needs to be decided by trial and error. The population is usually defined as a two dimensional array of –  size population, size x, chromosome size . Population Initialization There are two primary methods to initialize a population in a GA. They are − Random Initialization  − Populate the initial population with completely random solutions. Heuristic initialization  − Populate the initial population using a known heuristic for the problem. It has been observed that the e...

C++ (Object and Class)

The major purpose of C++ programming is to introduce the concept of object orientation to the C programming language. Object Oriented Programming is a paradigm that provides many concepts such as  inheritance, data binding, polymorphism etc. The programming paradigm where everything is represented as an object is known as truly object-oriented programming language.  Smalltalk  is considered as the first truly object-oriented programming language. OOPs (Object Oriented Programming System) Object  means a real word entity such as pen, chair, table etc.  Object-Oriented Programming  is a methodology or paradigm to design a program using classes and objects. It simplifies the software development and maintenance by providing some concepts: Object Class Inheritance Polymorphism Abstraction Encapsulation C++ Object In C++, Object is a real world entity, for example, chair, car, pen, mobile, laptop etc. In other words, object is an ent...