Mar 9, 2009

C++ program to add two numbers several times:

Adding two numbers in C++ several timesif you already know that how to add two given numbers in C++, we can then modify the program to add two given numbers several times. Generally we compile and run a C++ program by pressing CTRL+F9 key in case of TC (Turbo C++). if you don't have a Turbo compiler then Download Turboc C++ 3.0.

Generally The program runs only one set of data means program takes value of a and value of b then gives addition of c as output. If we would like to add more values we need to run/execute the program again. To overcome this problem the program below, adds two numbers several times until you give 0 (zero) value for a.

Here is the program :

#include<iostream.h>
#include<conio.h>
main()
{
clrscr();
int a,b,c;
for (int i=1; a!=0; i++)
{
cout <<"enter any number";
cin >> a;
cout <<"enter any number";
cin >> b;
c=a+b;
cout << "Value of C is="<<c <<endl;
cout << "------------------------"<<endl;
getch();
}
}


Here in the above program for loop is used to make the condition to take several data until 0 value of "a" variable is entered. The above program can be used to add two numbers many times.

No comments:

Post a Comment

Infolinks In Text Ads

Total Pageviews

Powered by Blogger.

Dont Forget To Follow Us

Blog Archive