1.5 09: Sum of odd numbers

Description
Calculate the sum of all odd numbers between non-negative integers m to n (including m and n), where m is not greater than n, and n is not greater than 300. For example, m=3, n=12, the sum is: 3+5+7+9+11=35.

Enter
two numbers m and n, separated by a space, where 0 <= m <= n <= 300.
Output
Output a line containing an integer representing the sum of all odd numbers between m and n (including m and n).
Sample input
7 15
sample output
55

#include <iostream>
using namespace std;
int main()
{
    
    
	int m, n, r, sum=0, temp;
	cin >> m >> n;
	for(int i=m;i<=n;i++)
	{
    
    
		if(i%2==1)
		{
    
    
			sum+=i;
		}
	}
	cout<<sum<<endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/yansuifeng1126/article/details/111994337