program to find divisors of 1-50

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jimix
    New Member
    • Feb 2008
    • 8

    program to find divisors of 1-50

    I have written the program for factors of #'s 1-50 but the factors print out below the number being factored. I can't figure out how to make it all on the same line.
    i.e. (now) divisors of 25 are
    1
    5
    25
    (need) divisors of 25 are 1, 5, 25.
    here is my code;
    [CODE=c]#include <stdio.h>


    int main()

    {
    int i;
    int x=50;
    int j;
    int k=50;

    for (i=1; i<=x; i++)
    { printf("The divisors of %d are ",i);
    for (j=1; j<=k; j++)
    if(i%j==0)
    printf("%d",j);

    }
    }[/CODE]
    Last edited by Ganon11; Feb 13 '08, 10:10 PM. Reason: Please use the [CODE] tags provided.
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Actually, this is what I get as output from your program:

    Code:
    $ gcc -o test test.c
    $ ./test
    The divisors of 1 are 1The divisors of 2 are 12The divisors of 3 are 13The divis
    ors of 4 are 124The divisors of 5 are 15The divisors of 6 are 1236The divisors o
    f 7 are 17The divisors of 8 are 1248The divisors of 9 are 139The divisors of 10
    are 12510The divisors of 11 are 111The divisors of 12 are 1234612The divisors of
     13 are 113The divisors of 14 are 12714The divisors of 15 are 13515The divisors
    of 16 are 124816The divisors of 17 are 117The divisors of 18 are 1236918The divi
    sors of 19 are 119The divisors of 20 are 12451020The divisors of 21 are 13721The
     divisors of 22 are 121122The divisors of 23 are 123The divisors of 24 are 12346
    81224The divisors of 25 are 1525The divisors of 26 are 121326The divisors of 27
    are 13927The divisors of 28 are 12471428The divisors of 29 are 129The divisors o
    f 30 are 12356101530The divisors of 31 are 131The divisors of 32 are 12481632The
     divisors of 33 are 131133The divisors of 34 are 121734The divisors of 35 are 15
    735The divisors of 36 are 123469121836The divisors of 37 are 137The divisors of
    38 are 121938The divisors of 39 are 131339The divisors of 40 are 12458102040The
    divisors of 41 are 141The divisors of 42 are 12367142142The divisors of 43 are 1
    43The divisors of 44 are 124112244The divisors of 45 are 13591545The divisors of
     46 are 122346The divisors of 47 are 147The divisors of 48 are 12346812162448The
     divisors of 49 are 1749The divisors of 50 are 125102550$
    You never print a newline character, so all the numbers are jumbled up on the same line.

    Comment

    • jimix
      New Member
      • Feb 2008
      • 8

      #3
      o.k. I tried it several ways. I hope I can just insert a \n in the right spot to solve the problem but I can't figure out where or if it is possible.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Well, when do you want a newline to appear? If you want all the divisors to appear on one line, then you should only print a newline character once all the divisors have been displayed.

        Comment

        Working...