By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,264 Members | 1,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,264 IT Pros & Developers. It's quick & easy.

stop the loop

P: 5
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
Share this Question
Share on Google+
1 Reply


P: 61
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.