# Why does the loop in my program turn infinite?

Printable View

• 07-07-2008
DaniiChris
Why does the loop in my program turn infinite?
With no good reason I know..........the loop in here turn infinite
can anybody know why

Code:

```#include <stdio.h> int main() { int x,ul,ll,j=1; printf("Please enter your desired number you want its divisible numbers\n"); scanf("%i",&x); printf("Please enter the lower limit you desire\n"); scanf("%i",&ll); //ALERT: getchar(); --> its only with characters! printf("Please enter the upper limit you desire\n"); scanf("%i",&ul);         for (j >= ll ; j <= ul; j--)         {                 if (j < x)                 {         if (j % x  == 0)                         {                                 printf("\n%i", x);                                 j++;                         }                 }                 else                 {      if ( j == x)                         {                                 j = 0;                                 printf("\t\t");                         }                 }         }         return (0); }```
• 07-07-2008
MacGyver
Code:

`for (j >= ll ; j <= ul; j--)`
You're setting j to be 0 or 1 depending on if it's equal or not to ll. Is this what you want? I think you really want to set it to the lower bound.

Also, you're going backwards by decrementing the value of j. This means if you start at 0 or 1, it'll go backwards to -1, -2, -3, -4, until underflow will most likely occur and you come back to whatever your upper bound is. Technically, this isn't an infinite loop if underflow occurs, but it might take kind of long as you probably found out. You probably want to from from lower bound to upper bound, so an increment makes more sense.
• 07-07-2008
Anuradh_a
@DaniiChris
I think this code will work for you!!!!

Code:

```#include <stdio.h> int main() { int x,ul,ll,j; printf("Please enter your desired number you want its divisible numbers\n"); scanf("%i",&x); printf("Please enter the lower limit you desire\n"); scanf("%i",&ll); //ALERT: getchar(); --> its only with characters! printf("Please enter the upper limit you desire\n"); scanf("%i",&ul);         for (j=0;j<ul;j++)         {                 if((j>ll)&&(x < j))                 {                 if (j%x == 0)                 printf("\n%d\n",j);                        }                         }                                 return 0; }```
• 07-07-2008
Cactus_Hugger
Code:

`int x,ul,ll,j=1;`
Single / two letter variables names are bad enough, but the variable name in red is especially bad. Find descriptive names for your variables - The first time I read your code I read that as "11" not "ll", and in the forum's font, thats:
Code:

```11 ll```
...nearly identical.
• 07-07-2008
zacs7
Not to mention ll stands for "long long",

ie,
Code:

`ll = (1298ul*ul) + (148192ll*ll);`
Gets rather confusing, another good reason not to use 'll' or even 'ul' as a variable name :)
• 07-07-2008
laserlight
Out of curiosity, DaniiChris, but after seeing my example in Displaying Numbers divisible by user's desired number , why did you revert to your version? If you do not understand the code, just ask for an explanation.

If you want to use the brute force method instead of Salem's idea, then fine, but I posted my example partially to give you an idea of what readable code looks like, so try and follow it.
• 07-08-2008
DaniiChris
Sure lasernight...Thanks a million! You are like my online teacher :D :D
Thanks to all :D :D
MacGyuver and Lasernight !
I am grateful! :D
Thanks to all :D :D