# Best way to line up numbers?

Page 1 of 1

## 6 Replies - 517 Views - Last Post: 28 January 2013 - 10:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=309992&amp;s=eedf972b4e004d0e46eaee10089cfd7c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

# Best way to line up numbers?

Posted 28 January 2013 - 08:25 PM

#include <stdio.h> //library for using input/output in the function
#include <conio.h> //needed to use getch() command

int main()
{
int x, y; //variables chosen to use in my function

printf("Enter the digit:\n");
scanf("%d", &x); //allows you to enter value for x and stores it

printf("The multiplication table for %d is:\n", x);
y = x * 1;
printf(" 1 x %d = %3d\n", x, y);
y = x * 2;
printf(" 2 x %d = %3d\n", x, y);
y = x * 3;
printf(" 3 x %d = %3d\n", x, y);
y = x * 4;
printf(" 4 x %d = %3d\n", x, y);
y = x * 5;
printf(" 5 x %d = %3d\n", x, y);
y = x * 6;
printf(" 6 x %d = %3d\n", x, y); //Number between % and d indicates how many spaces will be between the '=' and the %d value, thus lining up the numbers
y = x * 7;
printf(" 7 x %d = %3d\n", x, y); // printf statements display numbers 1 - 10 being multiplied by x to get y, thus a multiplication table
y = x * 8;
printf(" 8 x %d = %3d\n", x, y); // space added before each statement for numbers 1 - 9 to line up the 'x' and the '=' symbols for any number
y = x * 9;
printf(" 9 x %d = %3d\n", x, y);
y = x * 10;
printf("10 x %d = %3d\n", x, y);

getch(); //keeps program open until you press one more key
return(0);
}

My problem is that I need to get the number on the right side of the '=' sign to line up like:
5
55
555
5555
etc...

right now doing the %3d kinda works, but when you get to 4 digit numbers it no longer does. How do I get them lined up like I need to all the time no matter what number is entered?

I looked into the setw() thing, but not quite sure how that works out... I don't know how to get my printf statements to print in a setw() command nor do I know what number I need to enter into the () />

Any help would be awesome!

Is This A Good Question/Topic? 0

## Replies To: Best way to line up numbers?

• D.I.C Lover

Reputation: 331
• Posts: 1,168
• Joined: 01-April 11

## Re: Best way to line up numbers?

Posted 28 January 2013 - 08:46 PM

Try using the format specifiers in printf(). An example:
for(int i=0;i<100;i++)
printf("%6d  ",i);

The gives a field width of 6 digits, with 2 spaces after it. Since 80/8 equals 10, it shows 10 numbers per row, evenly spaced, and properly aligned.

By adding a - before the 6 in the above format, you can have printf() put the number on the opposite side of the default placement, within the field.

You have complete format control with printf(), but you need to learn to use the features it supports for formatting - and keep it handy for reference.

### #3 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Best way to line up numbers?

Posted 28 January 2013 - 09:16 PM

Well, I don't understand why you are trying to hard code your multiplication table. Wouldn't it be much simpler using loops like this.

Let the number of the multiplication table be x//int x;

accept value of x from user.

for(int i=1; i<=10;i++)
{
printf("%d * %d = %d ", x,i,(x*i));
}

See, how loops can aid you in simplifying your code drastically...

regards,
Raghav

This post has been edited by raghav.naganathan: 28 January 2013 - 09:17 PM

### #4 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

## Re: Best way to line up numbers?

Posted 28 January 2013 - 09:35 PM

Lol eh... I don't really understand loops and how to use them fully quite yet... So it is not easier at the moment.

But in this topic... can I get help with another issue? instead of starting a new thread...

#include <stdio.h>
#include <math.h>
#include <conio.h>

int main()
{
float a, b, h, p, v, r;
float pi = 3.14159265;

printf("Enter the base of the isosceles triangle:\n");
scanf("%f", &B)/>;
printf("Enter the height of the isosceles triangle:\n");
scanf("%f", &h);

a = (b * h) / 2;
p = b + (2 * a);
r = (1 / 2) * b;
v = (1 / 3) * (M_PI * r * r) * h;

printf("The area of the triangle is %.3f\n", a);
printf("The perimeter of the triangle is %.3f\n", p);
printf("The volume of the cone formed by spinning the triangle is %.3f", v);

getch();
return(0);
}

I have absolutely no idea what is wrong with my equations... I get 357.000 for perimeter and 0.000 for volume all the time! I cannot figure out what is wrong with it to not compute p and v

### #5 GunnerInc

• "Hurry up and wait"

Reputation: 910
• Posts: 2,357
• Joined: 28-March 11

## Re: Best way to line up numbers?

Posted 28 January 2013 - 09:50 PM

### #6 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,449
• Joined: 14-September 12

## Re: Best way to line up numbers?

Posted 28 January 2013 - 09:53 PM

Ah...that is happening because your radius is getting the value of 0.

Do the following changes and it should work fine.

r = b/2.0;
v = (pi* r * r * h)/3.0;

regards,
Raghav

This post has been edited by raghav.naganathan: 28 January 2013 - 09:53 PM

### #7 Bawnawgwa

Reputation: 9
• Posts: 153
• Joined: 21-January 13

## Re: Best way to line up numbers?

Posted 28 January 2013 - 10:57 PM

Ooh cool thank you. That fixed it right up!