C Program to find the sum of first n odd and even numbers using for
Program
#include<stdio.h>
void main()
{
int n, i, even_sum, odd_sum;
even_sum = 0;
odd_sum = 0;
printf("Enter the value of n:\t");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
if((i %2) == 0)
even_sum = even_sum + i;
else
odd_sum = odd_sum + i;
}
printf("Sum of even series from 1 to %d is %d\n", n, even_sum);
printf("Sum of odd series from 1 to %d is %d\n", n, odd_sum);
}
Output
Enter the value of n: 22
Sum of even series from 1 to 22 is 132
Sum of odd series from 1 to 22 is 121