C++ Program to print the sum of n odd numbers within range using for
Program
#include <iostream>
using namespace std;
int main()
{
int start_range, end_range;
int sum = 0;
cout << "Enter the starting range: ";
cin >> start_range;
cout << "Enter the ending range: ";
cin >> end_range;
cout << "Sum of all odd numbers in given range is :";
for (int i = start_range; i <= end_range; i++)
if (i % 2 != 0)
sum = sum + i;
cout << sum;
return 0;
}
Output
$ g++ sum-of-n-odd-numbers-within-range-using-for.cpp
$ ./a.out
Enter the starting range: 3
Enter the ending range: 15
Sum of all odd numbers in given range is: 63