Sunday, June 8, 2008

Add integers till EOF

/*Write a program to keep on accepting numbers(integers) & add them till EOF(End of file == -1 or ctrl + z ) isnt pressed.On press it returns sum of all integers entered */


Soln.Code :-
========

#include <stdio.h>

int main(void)
{
int x = 0,y = 0;
printf("Enter your integer now\n");
printf("Enter one integer per line & then press Enter\n");
printf(">>>");
while (((scanf("%d",&y))!= EOF))
{
x = x + y;
printf(">>>");
}
printf("\n The sum of X is %d\n",x);
return 0;

}


No comments: