In this article we are going to write a C program for calculating sum of odd and even number in range 1 to n.
C Program for calculating sum of even and odd numbers
C Program
#include<stdio.h>
void main()
{
int n,i,sum1=0,sum2=0;
printf("enter the number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
sum1=sum1+i;
else
sum2=sum2+i;
}
printf("sum of even numbers = %d\n\n",sum1);
printf("sum of odd numbers = %d\n\n",sum2);
}
Output
Similar C Programs
Sum and Average in C – https://gangforcode.com/c-program-for-taking-input-of-marks-and-calculating-sum-and-average/
Thanks !!!!!
6 thoughts on “C Program to Print Sum of Even and Odd Numbers”