Printing columns in C
There are similar questions, but nobody is helping me find the answer I'm looking for, so I came to ask for help!
Also, this is homework, so I'm looking for an explanation for more than just working code.
I need to print two or perhaps four of its columns to display factorials for numbers 1-20.
I'm pretty close, I just can't figure out how to get these two parts 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 this is 4 columns and I need to sort it out somehow to print
1! = 1 11! = 39961800
Or am I just missing something in my printf?
Any guidance is greatly appreciated.
source to share
Printing 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);
}
source to share
Once you use a newline character, you cannot go back to the previous line (unchanged).
If you need 1! and 11! on one line they must be printed on one line followed by a newline character. So, for a particular iteration, the factorials i and (10 + i) will be printed. (1 <= & i l; = 10).
Also, to store factorials over 12, you will need a long datatype.
source to share
For this you need to print f to see something like this
printf("%d! = %-20lu "%20d ! = %lu", intIndex, lngFactorialn, (intIndex + 10), lngFactorialn);
so you need to calculate them first and then print them out at the same time. I think this should do the trick with just one loop.
source to share
As other answers, you should calculate the same rows in each column at the same time. Here is my solution that works for different column numbers:
#include <stdio.h>
#include <string.h>
void factorial_in_columns(int col_num, int width, int max_fact) {
int col_width = width / col_num; /* the width of a column */
int fact_offset = max_fact / col_num; /* calculation distance */
char col_buffer[col_width]; /* buffer of a column */
char line_buffer[width + 1]; /* buffer of a line */
long factorial;
int i, j, k, x, len;
for(i = 1; i <= fact_offset; ++i) {
for(j = 0; j < col_num; ++j) {
x = (i + (j * fact_offset)); /* current number */
for(factorial = 1, k = 1; k <= x; ++k)
factorial *= k;
/* convert the result to string and append it to the line buffer */
snprintf(col_buffer, col_width, "%d = %ld", x, factorial);
sprintf(line_buffer + (col_width * j), "%-*s", col_width, col_buffer);
}
printf("%s\n", line_buffer);
}
}
int main() {
factorial_in_columns(4, 120, 20);
return 0;
}
source to share