Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hello internet,
i have a problem with my program again sorry for asking so many questions. my problem is that my program will count to one for than i want if i set the starting number at two and increment to count by at two and set the end number at 16 it will end up counting to 18. Thanks for taking the time and looking at my question.


C#
#include <stdio.h>
int main(void)
{

unsigned int s;
int i;
int e;

printf("insert starting number: ");
scanf("%d",&s);

printf("insert number to count by: ");
scanf("%d",&i);

printf("insert number to stop counting to: ");
scanf("%d",&e);

while (s<=e)
{
printf("%u\n",s+=i);
}
return 0;
}
Posted

1 solution

Hi,

The problem you are having is that the exit condition of the while loop is <=, and in this case s = 16, and e = 16. You then increment s by i after it is equal to 16. If your exit condition is <, it should work the way you expect. You do need to be careful if your increment is greater than one; it can still put it past e. You might want to check if s+i <= e instead.
 
Share this answer
 
Comments
CPallini 16-Mar-15 13:42pm    
5.
sparton07sev 16-Mar-15 15:18pm    
thanks for the help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900