 user3691838 -4 years ago 94
C Question

# Printing Columns in C

There are similar questions asked, but none that help me arrive at the answer I'm looking for, so I've come to ask for help!

Also, this is homework so I am looking for an explanation more so than just working code.

I need to print two, or maybe its four, columns of output to display the factorials for the numbers 1-20.

I'm pretty close, I just can't seem to figure out how to get these two pieces to be side by side.

Code:

``````lngFactorial = 1;

// Factorials 1 - 10
for (intIndex = 1; intIndex <=10; intIndex += 1)
{

lngFactorial *= intIndex;
printf("%d! = %-20lu \n", intIndex, lngFactorial );
}

// Factorials 11 - 20
for (intIndex = 11; intIndex <= 20; intIndex += 1)
{
lngFactorial *= intIndex;
printf("%20d ! = %lu \n", intIndex, lngFactorial);
}
``````

Output:

``````1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11 ! = 39916800
12 ! = 479001600
13 ! = 1932053504
14 ! = 1278945280
15 ! = 2004310016
16 ! = 2004189184
17 ! = 4006445056
18 ! = 3396534272
19 ! = 109641728
20 ! = 2192834560
``````

So, is this 4 columns and I need to figure out somehow to to print

``````1! = 1    11! = 39961800
``````

Or am I just missing something in my printf?

Any guidance is much appreciated. tilz0R
Answer Source

Print columns together, something like

``````// Using long long to allow big numbers and to prevent overflow.
unsigned long long lngFactorial1 = 1;
unsigned long long lngFactorial2 = 3628800;// 10!
for (intIndex = 1; intIndex <= 10; intIndex++) {
lngFactorial1 *= intIndex;
lngFactorial2 *= intIndex + 10;
printf("%d! = %-20llu ; %d! = %-20llu;\n", intIndex, lngFactorial1, intIndex + 10, lngFactorial2);
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download
Latest added