# having trouble solving a program

• 05-27-2010
everyone0
having trouble solving a program
Hi all, i was having trouble solving this question:

Write a C program that displays all numbers from 1 to Y,which are divisible by a given number X, Where the user enters the values of X and Y.

ex: if the entered values of X and Y are 3 and 30 respectively ,the program will produce the following output:

" Numbers from 1 to 30 divisible by 3 are : 3 6 9 12 15 18 21 24 27 30"

here is my work so far , can anyone help me with the rest of the program or correct the loop for me ?

Code:

```#include <stdio.h> #include <conio.h> main() { int X,Y,i; printf("Please Enter value of  X,Y"); scanf("%d%d",&X,&Y); for(i=1;i<=Y;i++) printf("Numbers from 1 to %d divisible by 3 are : %d",Y,i); getch(); }```
• 05-27-2010
KBriggs
Well, you don't have the logic down yet. You are just printing out all the numbers between 1 and y.

You need to test if they are divisible by x before you print them.
• 05-27-2010
claudiu
So just test if i is divisible by x. If it is print it . Think about the definition of divisibility in order to deduce what your testing condition should be.
• 05-27-2010
everyone0
how do i test if a no. divided a no. gives an int not a float value ? do i use the % ? help plz
• 05-27-2010
everyone0
Code:

```#include <stdio.h> #include <conio.h> main() { int X,Y,i; printf("Please Enter value of  X,Y"); scanf("%d%d",&X,&Y); while(i%X==0) for(i=1;i<=Y;i++) printf("Numbers from 1 to %d divisible by 3 are : %d",Y,i); getch(); }```
but still not printing what i need , any adjustments please ?
• 05-27-2010
claudiu
Quote:

Originally Posted by everyone0
how do i test if a no. divided a no. gives an int not a float value ? do i use the % ? help plz

Yes you use %. A number is divisible by another number if the remainder of the division of that number by the second number is 0. Test for that in an if statement inside your for loop and you are done.
• 05-27-2010
everyone0
i did test for that , thanks but can u tell me how to give this exact printout ? " Numbers from 1 to 30 divisible by 3 are : 3 6 9 12 15 18 21 24 27 30" cause my printf dont do it
• 05-27-2010
KBriggs
• 05-27-2010
everyone0
```for( x = 0; x < 100; x++ ) {     if( this % that == thisotherthing )         printf( "%d %% %d has %d has a remainder\n", this, that thisotherthing ); }```