| Previous | Home | Next |
To find sum of even and odd enter a number from keyboard. Use for loop to iterate from 1 to n. Use if -else to check the divisibility of 2. If diviseble by two then add number into even else add number into odd. Finally display sum of odd and even.
/* Print the sum of odd number and even number separately of n number */
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,even=0,odd=0;
clrscr();
printf("Enter a number\t");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
if(i%2==0)
even=even+i;
else
odd=odd+i;
}
printf("\nsum of the even\t=%d",even);
printf("\nsum of the odd\t=%d",odd);
getch();
}
Enter a number 12
sum of the even =42
sum of the odd =36
| Previous | Home | Next |