Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Best way to line up numbers?

$
0
0
#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!

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>