Skip to content

Commit 0bbcdc5

Browse files
committed
added: 11-print_to_98.c
1 parent 1391cd3 commit 0bbcdc5

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "main.h"
2+
3+
/**
4+
* checkDigits - check num of digits.
5+
*
6+
* @n: number.
7+
*
8+
* Return: return nothing.
9+
*/
10+
void checkDigits(int n)
11+
{
12+
int singleDigit;
13+
int base = 10;
14+
15+
while (n >= base)
16+
{
17+
base = base * 10;
18+
}
19+
while (base != 1)
20+
{
21+
singleDigit = (n % base) / (base / 10);
22+
_putchar(singleDigit + '0');
23+
base = base / 10;
24+
25+
}
26+
}
27+
28+
/**
29+
* print_to_98 - prints all natural number from `n` to 98,
30+
* followed by a new line.
31+
*
32+
* @n: the start number.
33+
*
34+
* Return: return nothing.
35+
*/
36+
void print_to_98(int n)
37+
{
38+
while (n != 98)
39+
{
40+
if (n >= 0 && n <= 98)
41+
{
42+
checkDigits(n);
43+
n++;
44+
}
45+
else if (n < 0)
46+
{
47+
_putchar('-');
48+
checkDigits(n * -1);
49+
n++;
50+
}
51+
else
52+
{
53+
checkDigits(n);
54+
n--;
55+
}
56+
57+
_putchar(',');
58+
_putchar(' ');
59+
}
60+
checkDigits(98);
61+
_putchar('\n');
62+
}

0x02-functions_nested_loops/main.h

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ int print_last_digit(int);
99
void jack_bauer(void);
1010
void times_table(void);
1111
int add(int, int);
12+
void print_to_98(int);

0 commit comments

Comments
 (0)