Thread: having trouble solving a program

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    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();
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    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.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    how do i test if a no. divided a no. gives an int not a float value ? do i use the % ? help plz

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    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 ?

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by everyone0 View Post
    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.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    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

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    post your code with your new test condition included.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    did that already lol

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    If the numbers are "divisible by N", why not just make the loop increment by that amount?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're doing it wrong. You don't use a loop with % foo == 0, you use that as a check:
    Code:
    for( x = 0; x < 100; x++ )
    {
        if( this % that == thisotherthing )
            printf( "%d %% %d has %d has a remainder\n", this, that thisotherthing );
    }
    You aren't actually thinking about what you are doing, you're just throwing lines of code in and hoping it does what you want.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trouble with program
    By kblanch09 in forum C Programming
    Replies: 3
    Last Post: 03-31-2010, 03:30 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  3. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  4. big trouble in little program
    By jonesy in forum C Programming
    Replies: 1
    Last Post: 10-04-2001, 02:04 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM