-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_int.c
67 lines (58 loc) · 957 Bytes
/
print_int.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
#include "main.h"
/**
* print_number - prints integer without handling negative
* @n: number
*
* Return: Number of characters printed
*/
int print_number(int n)
{
int char_count = 0;
int digit, divisor, i, tmp, num_of_digits;
if (n == 0)
{
_putchar('0');
char_count++;
}
else
{
num_of_digits = 0;
tmp = n;
while (tmp > 0)
{
tmp /= 10;
num_of_digits++;
}
divisor = 1;
for (i = 1; i < num_of_digits; i++)
divisor *= 10;
while (divisor > 0)
{
digit = n / divisor;
_putchar(digit + '0');
n %= divisor;
divisor /= 10;
char_count++;
}
}
return (char_count);
}
/**
* print_int - prints integer
*
* @argument_list: argument to print
* Return: Number of characters printed
*/
int print_int(va_list argument_list)
{
int n = va_arg(argument_list, int);
int char_count = 0;
if (n < 0)
{
_putchar('-');
char_count++;
n = -n;
}
char_count += print_number(n);
return (char_count);
}