Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A simple question of alignment of columns.

  1. Apr 17, 2007 #1
    my assignment is as follows:
    write a programme printing a reasonably sized table of powers, in a nice format. using %nd in printf will write an integer in at least n charcaters. this facilitates printing tables with aligned columns.
    here's my code:
    Code (Text):

    #include <stdio.h>
    #define STOP 10
    int base()
      int input1;
      printf("enter the power to which you want to raise the base \n");
      printf("enter %d to stop \n", STOP);
      scanf("%d", &input1);
      int c,m=1,p=1,b=base();
        while (b<STOP){
            printf("\n %d", p);
    now i want to align them in a table, so im not sure how to use here %nd, any help would be appreciated.
  2. jcsd
  3. Apr 17, 2007 #2


    User Avatar
    Science Advisor
    Homework Helper

    The "n" in "%nd" is a number, not the letter n.

    For example "%3d" means print the number using at least 3 characters. So 5 would print as "space space 5", 25 as "space 5", "125" as "125".

    "n" doesn't have to be a single digit. "%20d" would use 20 characters to print your number if you want to be really "spaced out".

    There are more options if you need them, e.g. left justified numbers, printing "+" before positive numbers, adding zeros so 7 prints as "007", etc, etc. Look up "printf format modifiers" in your documentation (or on Google) for the details.
  4. Apr 18, 2007 #3
    yes i know that it's a number, but i dont see how does it help me to biuld a table of powers.
    i want that in the first row there will be only powers of 1, i.e:
    1 2 3 4 5 6 7 8 9
    1 4 9 16 36 49 64 81
    ............................ etc
    the problem is i dont see how %(number)d can help me?
  5. Apr 18, 2007 #4


    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Consider the largest number you're going to print. Maybe it's 8 digits? Then you'd use %8d to print each number, and each number would occupy 8 columns worth of space.

    - Warren
  6. Apr 18, 2007 #5
    loop quantum gravity, you might want to consider the for loop instead of the while here.
    In addition, since base() is only used once there is no good reason to make it into a separate function.
    Last edited: Apr 18, 2007
  7. Apr 18, 2007 #6


    User Avatar
    Science Advisor
    Homework Helper

    To do that, you need to take the "\n" out of your existing printf() call, then add another printf("\n") statement in the right place, so you only output a new line after each complete row of numbers.

    Changing "%d" to "%5d" or whatever will space the numbers neatly within each row.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: A simple question of alignment of columns.
  1. Simple python question (Replies: 2)