Thread: Listing factors in a grid.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    5

    Listing factors in a grid.

    Currently, I'm attempting to output the factors of a number in a grid format as opposed to just a direct line of numbers.

    I am trying to use a for loop to accomplish this, however, i'm having a difficult time deducting a formula to create this.

    Any input on how to make the output into a grid would be awesome. Thanks.

    Code:
    for(i=1;i<=n;i++)
    		{
    		   if(n%i==0)
    		   cout << n/i <<endl;		
    		}

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you mean by grid format? How do you want your grid to look?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Say n=64

    64 32 16 8 4
    2 1

    instead of

    64
    32
    16
    8
    4
    2
    1

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So if you just want a line break after every n numbers, then print a line break after every n numbers. (Use a counter to count how many you've printed on the current line, and when you reach the max print a new line and start over.)

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    Thanks. How many for loops would I need to go about doing that?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No special for loops other than the one you've already got that's printing all the numbers.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    5
    thank you! got it to work using a while statement as a counter, and I replaced the endl; statement in there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More Windows Trouble
    By samGwilliam in forum Windows Programming
    Replies: 9
    Last Post: 04-12-2005, 10:02 AM
  2. Find a word in a 2d grid
    By The_Kingpin in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2005, 05:38 PM
  3. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Help with homework please
    By vleonte in forum C Programming
    Replies: 20
    Last Post: 10-13-2003, 11:16 AM