While Statement 1


Write a program that sums n numbers from arbitrary defined starting number.
sum[n] where n is number from 0 to i. (i is a arbitrary defined limit)
// Defines the entry point for the console application.
//Program for summing n numbers.

#include "stdafx.h"
#include "iostream"
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
            int a, b = a, n;
            cout << "Enter the first value of the sum a: " << "\n";
            cin >> a;
            cout << "Enter the maximum nuber of iterations n: " << "\n";
            cin >> n;
            while (a < n) {
                        ++a;
                        b += a;
            }
            cout << "The value of first " << n << " numbers is: " << b << "\n";
            system("pause");
            return 0;
}


After we written our code, start the compiler. On command prompt screen you shoud enter first number of the sume in this case we’ve chosen 0. The next step is to declare a number of iterations. The result is given below.

As you can see the sum of first 5 numbers is:

1 + 2 + 3 + 4 + 5 = 15
Share on Google Plus