468,512 Members | 1,512 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

stop the loop

I kept creating an infinite loop...
Intro level problem; should be easy




Given that two int variables, total and amount, have been declared, write a loop that reads integers into amount and adds all the non-negative values into total. The loop terminates when a value less than 0 is read into amount. Don't forget to initialize total to 0.
Sep 18 '06 #1
1 8254
Try this one.......

main()
{
int amount, total=0;

while(1)
{
printf("Entera number ");
scanf("%d", &amount);

if(amount < 0)
break;

total=total+amount;
}

printf("%d", total);
}
Sep 19 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Steve Henderson | last post: by
14 posts views Thread by S. Nurbe | last post: by
4 posts views Thread by Chip | last post: by
14 posts views Thread by electrician | last post: by
5 posts views Thread by =?Utf-8?B?Sm9uYXRoYW4gU21pdGg=?= | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.