C Program to print the sum of n odd and even numbers within range using for
Program
#include<stdio.h>
void main()
{
int i;
int start_range, end_range;
int o_sum = 0;
int e_sum = 0;
printf("Enter the starting range:\t");
scanf("%d", &start_range);
printf("Enter the ending range:\t");
scanf("%d", &end_range);
for (i = start_range; i <= end_range; i++)
if (i % 2 != 0)
{
o_sum = o_sum + i;
}
else
{
e_sum = e_sum + i;
}
printf("sum of all odd numbers in the given range is: %d\n", o_sum);
printf("sum of all even numbers in the given range is: %d\n", e_sum);
}
Output
$ gcc sum-of-n-odd-and-even-numbers-within-range-using-for.c
$ ./a.out
Enter the starting range: 25
Enter the ending range: 50
sum of all odd numbers in the given range is: 481
sum of all even numbers in the given range is: 494