Sunday 3 April 2011

Write a program in C to find the sum of odd numbers and even numbers from 1 to n

#include
#include

void main()
{
int n,i;
int addeven=0;
int addodd=1;

printf("\nEnter a number\n");
scanf("%d",&n);
fflush(stdin);

for(i=2;i<=n;i++)
{
if(i%2==0)
addeven+=i;
else
addodd+=i;
}

printf("\nThe sum of odd numbers is %d \nThe sum of even numbers is %d\n",addeven,addodd);
}


No comments:

Post a Comment