C Program to print the sum of n odd and even numbers using do while
Program
#include<stdio.h>
void main()
{
int i, n;
int o_sum = 0;
int e_sum = 0;
printf("Enter the value of n \n");
scanf("%d", &n);
i = 1;
do
{
if(i%2 != 0)
{
o_sum = o_sum + i;
}
else
{
e_sum = e_sum + i;
}
i++;
}while (i <= n);
printf("Sum of all odd numbers from 1 to %d is: %d\n", n, o_sum);
printf("Sum of all even numbers from 1 to %d is: %d", n, e_sum);
}
Output
$ gcc sum-of-n-odd-and-even-numbers-using-do-while.c
$ ./a.out
Enter the value of n
20
Sum of all odd numbers from 1 to 20 is: 100
Sum of all even numbers from 1 to 20 is: 110