Saturday, 18 March 2017

C Program to calculate the sum of even and odd numbers

C Program to calculate the sum of even and odd numbers is given below.


#include <stdio.h>
int main()
{
   int number,i,sum_odd=0,sum_even=0;

   printf("\nEnter the number: ");
   scanf("%d",&number);

   for(i=1;i<=number;i++)
   {
        if(i%2==0)
        {
             sum_even=sum_even+i;
        }
        else
        {
             sum_odd=sum_odd+i;
        }
   }

   printf("\nThe sum of even number is: %d",sum_even);
   printf("\nThe sum of odd number is: %d",sum_odd);
   printf("\n");
   return 0;
}


OUTPUT:

 


EmoticonEmoticon