Visual C++ - Sum of ODD Numbers in the Given Range
Here is the Visual C++ program for the sum of ODD Numbers given a range.
Source Code
#include <iostream>
void main()
{
int index, begno, endno, sum = 0;
std::cout << "Program for sum of odd numbers in the given range\n";
std::cout << "Enter Beg. No.: ";
std::cin >> begno;
std::cout << "Enter End. No.: ";
std::cin >> endno;
index = begno;
if( (begno % 2) == 0) // If it even, then make it ODD
index = begno + 1;
for(; index <= endno; index += 2)
{
sum = sum + index;
}
std::cout << "The sum of odd numbers between " << begno << " and " << endno << " is: " << sum << "\n";
}
Output
Program for sum of odd numbers in the given range
Enter Beg. No.: 1
Enter End. No.: 100
The sum of odd numbers between 1 and 100 is: 2500
|