print the total of 1-2+3-4+5-6+7-8+9-10...

/* print the total of 1-2+3-4+5-6+7-8+9-10 */

#include
#include
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
{
if(i==1)
{
printf("%d",i);
sum=sum+i;
}
else if(i%2==0)
{
printf("-%d",i);
sum=sum-i;
}
else
{
printf("+%d",i);
sum=sum+i;
}
}
printf("\n Total = %d",sum);
getch();
}

0 comments:

Post a Comment