Monday, May 26, 2014

C++ Program to Perform Several Mathematical Operations at a Time [C++ 03]


Objectives:
-to Perform Addition, Subtraction, Multiplication and Division at a time
-to Know when to use the command float
-to Know how to use the command float
-to Know how to use a line break
-to Learn to show several results one after another


Short description about the program:
The C++ Program given below is able to perform four mathematical operations at a time. Though it will show all the results at a time but the program will actually do that one after another. From the previous program we have learnt to add two numbers. But in this program we will run four mathematical operations. Into the fifth line of this program, we have to use two variables n1 and n2 to store two numbers we will use later. And the rest four variables add, sub, mul and div are respectively for addition, subtraction, multiplication and division. Actually the result after the mathematical operations will be kept into those variables. In this program, we have declared float type variables. Because, we will perform division that may result float type data.

 01 #include<iostream>
 02 using namespace std;
 03 int main()
 04 {
 05 float n1, n2, add, sub, mul, div;
 06 n1=5;
 07 n2=6;
 08 add=n1+n2;
 09 cout<<"The Summation is: ";
 10 cout<<add;
 11 sub=n1-n2;
 12 cout<<"\nThe Substraction is: ";
 13 cout<<sub;
 14 mul=n1*n2;
 15 cout<<"\nThe Multiplication is: "; 
 16 cout<<mul;
 17 div=n1/n2;
 18 cout<<"\nThe Division is: ";
 19 cout<<div;
 20 }

Description of the above codes

Line 05: float n1, n2, add, sub, mul, div:
This is the fifth line of the above program. We have declared six float type variables to perform the operations accurately. The operation division may result float type data. That's why we have to declare the variables as float type. Otherwise the result may not be accurate.
Line 11: sub=n1-n2:
This eleventh line is new in this program. In this line there is told to subtract n2 from n1 by the command n1-n2 and at same time the processor will keep the result into the variable sub.

In fourteenth and seventeenth lines we did almost the same. In fourteenth line we have told the processor to multiply n1 and n2 and to keep the result into the variable mul.

Later it the seventeenth line we have told the processor to divide n1 by n2 and to keep the result into the variable div.

All the rest lines are described into the previous post. In this program we have used \n to create a new line only. \n will work as like a line break as we get after pressing the Enter Key.


Another C++ Programs



No comments:

Post a Comment