ex-04: Initial solution
This commit is contained in:
parent
a509424d19
commit
710747013d
61
exercise-04-multiplication-table/main.c
Normal file
61
exercise-04-multiplication-table/main.c
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int range;
|
||||||
|
|
||||||
|
printf("Rozsah:\n");
|
||||||
|
if (scanf("%d", &range) != 1) {
|
||||||
|
printf("Nespravny vstup.\n");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int largest_number = range * range;
|
||||||
|
int largest_digit_count = 0;
|
||||||
|
int range_digit_count = 0;
|
||||||
|
|
||||||
|
|
||||||
|
int tmp = largest_number;
|
||||||
|
while(tmp != 0) {
|
||||||
|
largest_digit_count++;
|
||||||
|
tmp /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = range;
|
||||||
|
while(tmp != 0) {
|
||||||
|
range_digit_count++;
|
||||||
|
tmp /= 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Header
|
||||||
|
for(int i = range + 1; i > 0; i--) {
|
||||||
|
if(i == range + 1) {
|
||||||
|
printf("%*s|", largest_digit_count, "");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%*d", largest_digit_count + 1, i);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
// Separator
|
||||||
|
for(int j = 0; j < largest_digit_count; j++) {
|
||||||
|
putchar('-');
|
||||||
|
}
|
||||||
|
putchar('+');
|
||||||
|
|
||||||
|
for(int j = 0; j < (largest_digit_count + 1) * range; j++) {
|
||||||
|
putchar('-');
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
|
||||||
|
// Table lines
|
||||||
|
for(int line = 1; line <= range; line++) {
|
||||||
|
printf("%*d|", largest_digit_count, line);
|
||||||
|
|
||||||
|
for(int column = range; column >= line; column--) {
|
||||||
|
printf("%*d", largest_digit_count + 1, column * line);
|
||||||
|
}
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user