For loop assistance

closed account (oy721hU5)
Hello programmers... I am doing this for- and it is not printing the numbers (no output). Please tell me what is wrong with it. The numbers to print must be greater than ten and less than 999.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("\nNUMBERS ARE : " );
    for (k = 0; k < 10; k++)
    {
        Hold = rand();

        if ( Hold % 7 == 0  && Hold < 999 && Hold > 10 )
        {
            printf("%5d", Hold);
        }

    }

}
Last edited on
Hm... does this compile for you? Line 7 should complain about "k" not being defined. "Hold" is not defined either.
Last edited on
Look at line 9 you are giving hold a value between 0 and 2147483647

So the odds of it being between 10 and 999 are very slim. If you want to change the range of the random number to 10 and 999 try changing line 9 to

1
2
3
4
Hold = rand() % 1000; //random number between 0 and 999 then just 
//add ten to it on another line or put this:
//rand() % 1000 + 10; 
//now it is 10 - 999 


**edited below

then you dont exactly need the if statement on line 11 though I'm not sure why you are trying to see if the number is divisible by 7.
Last edited on
@giblit
Upper bound should be 990 initially, then add 10, correct?
1
2
3
4
Hold = rand() % 990; //random number between 0 and 989 then just 
//add ten to it on another line or put this:
//rand() % 990 + 10; 
//now it is 10 - 999  
Last edited on
Yes you are correct. Sorry it is in this format

rand() % ( Max - min + 1 ) + min;

So it would be

rand() % ( 999 - 10 + 1 ) + 10;

which is

rand() % 990 + 10;
Topic archived. No new replies allowed.