C Program to print the sum of n odd and even numbers within range using do while
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);
i = start_range;
do
{
if (i % 2 != 0)
{
o_sum = o_sum + i;
}
else
{
e_sum = e_sum + i;
}
i++;
} while (i <= end_range);
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-do-while.c
$ ./a.out
Enter the starting range: 5
Enter the ending range: 10
Sum of all odd numbers in the given range is: 21
Sum of all even numbers in the given range is: 24