Software & Finance





Visual C++ - Sum of ALL Numbers in the Given Range





Here is the Visual C++ program for the sum of ALL Numbers given a range.

 


Source Code


#include <iostream>

 

void main()

{

    int index, begno, endno, sum = 0;

    std::cout << "Program for sum of all numbers in the given range\n";

 

    std::cout << "Enter Beg. No.: ";

    std::cin >> begno;

    std::cout << "Enter End. No.: ";

    std::cin >> endno;

    index = begno;

   

    for(; index <= endno; index++)

        sum = sum + index;

 

    std::cout << "The sum of all numbers between " << begno << " and " << endno << " is: " << sum << "\n";

}

Output


 

Program for sum of all numbers in the given range

Enter Beg. No.: 1

Enter End. No.: 100

The sum of even numbers between 1 and 100 is: 5050