-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path100-times_table.c
93 lines (86 loc) · 1.2 KB
/
100-times_table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "main.h"
/**
* checkDigits - check num of digits.
*
* @n: number.
*
* Return: number of digits.
*/
int checkDigits(int n)
{
int digit = 0;
if (n == 0)
{
return (1);
}
while (n != 0)
{
n = n / 10;
digit++;
}
return (digit);
}
/**
* printDigits - print each digit individually.
*
* @n: number.
*
* Return: return nothing.
*/
void printDigits(int n)
{
int singleDigit;
int base = 10;
while (n >= base)
{
base = base * 10;
}
while (base != 1)
{
singleDigit = (n % base) / (base / 10);
_putchar(singleDigit + '0');
base = base / 10;
}
}
/**
* print_times_table - prints the 9 times table, starting with 0.
*
* @n: number of rows and columns.
*
* Return: return nothing.
*/
void print_times_table(int n)
{
int column = 0;
int row = 0;
int result;
int nextResult;
int nDigits;
if (n > 15 || n < 0)
{
return;
}
while (column <= n)
{
while (row <= n)
{
result = row * column;
nextResult = (row + 1) * column;
nDigits = checkDigits(nextResult);
printDigits(result);
if (row != n)
{
_putchar(',');
while (nDigits <= 3)
{
_putchar(' ');
nDigits++;
}
}
row++;
}
_putchar('\n');
column++;
row = 0;
}
}