Friday, January 3, 2014

C++ Program to Add two Numbers and to Show the Result [C++ 02]


Objectives
-to Understand the basic operation of a C++ Program
-to Know how to perform a mathematical operation using C++ Language
-to Learn how to declare  several variable and how to use them later
-to Know where to keep the result after mathematical operation and how to show the result
The C++ Program is given below is to add two numbers. The program will add two number we will provide during typing the codes. The addition of those two number will be kept in a variable named r and later it will be appear on the screen as a result.

//C++ Program to Add two Numbers
#include<iostream>
using namespace std;
int main()
{
int n1, n2, r;
n1=5; n2=6;
r=n1+n2;
cout<<"The Summation is: ";
cout<<r; return 0;
}

The Result of the above C++ Program: The Summation is: 11
The description of the above codes are given below...
Line 05: int n1, n2, r;
The fifth line is to declare three variables named n1, n2 and r. We will declare n1 to keep the first number and similarly n2 to keep the second number. And we will declare r to keep the result in it.

Line 06: n1=5;
We have defined a value of n1 in the sixth line. Actually we have told the processor to keep 5 into the variable n1. Similarly we have kept 6 into n2 variable using the seventh line n2=6;

Line 08: r=n1+n2;
In the eighth line there is told to add n1 and n2 by n1+n2 and at the same time the result of the addition will be kept into the variable r.

Line 09: cout<<"The Summation is: ";
The ninth line we will use to print a line The Summation is:  so that the summation can be added just after the line. This line will be used only to beautify the result. You may ignore this line.

Line 10: cout<<r;
We will print the result by this tenth line. This line will show the data kept into the variable r. The result will be added after the line The Summation is: 


No comments:

Post a Comment