(C++) CPP Programs - 6
Friday, December 14, 2007
6. Write a C program to find the sum of odd numbers and the sum of even numbers from 1 to N. Output the computed sums on 2 different lines with suitable messages.
/* Sum of even and odd numbers*/
#include
#include
void main()
{
int n,i,os=0,es=0;
clrscr();
printf("\nEnter value of N\n");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if(i%2==0)
es+=i;
else
os+=i;
}
printf("\nSum of even numbers=%d\n\a",es);
printf("\nSum of odd numbers=%d\n\a",os);
getch();
}
0 comments:
Post a Comment