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