Thread: printf formatting issue

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Question printf formatting issue

    I've written a program which takes a character string and then prints each character vertically so that for instance the string 123 can be written as
    1
    2
    3
    no what i need is for all the numbers from zero to the inputted number to print the numbers digits vertically but each number to be printed horizontally so that for instance an input of 11 prints
    1 2 3 4 5 6 7 8 9 1 1
    0 1

    i've made it so that i can print all numbers up to the inputted number vertically; however, i am stuck with a method for making each number print horizontally as described above.

    Any thoughts?

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Don't print '\n' in every iteration. Instead add a space to the format string or use a width specifier for even spacing.

    Code:
    printf("%d ", number);
    //or
    printf("%4d", number);

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    should have been
    1 2 3 4 5 6 7 8 9 1 1
    _______________0 1

    _ should be spaces
    Last edited by Bryan James; 02-19-2013 at 08:27 PM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Write it out step by step, by hand, on paper to try to figure out the algorithm first.

    If the number is greater than or equal to 10, how many spaces do you want to print out first in the second line?

    There's a solution in there that correlates well with your data. Your job, as a programmer, is to find and implement that solution.

    If you have specific questions along the way, post your code (in code tags) and ask them.

    Best of luck.

    (P.S. If you're not familiar with the modulus operator, then do some research to help you along the way.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with printf formatting
    By mop777 in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 12:52 PM
  2. Formatting with printf
    By samus250 in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 07:12 PM
  3. Formatting with Printf
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 04-15-2008, 09:18 AM
  4. Printf and variable formatting
    By divineleft in forum C Programming
    Replies: 2
    Last Post: 12-24-2006, 11:05 AM
  5. Formatting printf
    By Lah in forum C Programming
    Replies: 2
    Last Post: 09-11-2003, 11:44 PM

Tags for this Thread