-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_unsigned_int.c
97 lines (91 loc) · 1.54 KB
/
print_unsigned_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
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
94
95
96
97
#include "main.h"
/**
* print_u - prints an unsigned in in decimal notation
* @u: unsigned int to print
*
* Return: number of digits printed
*/
int print_u(va_list u)
{
unsigned int a[10];
unsigned int i, m, n, sum;
int count;
n = va_arg(u, unsigned int);
m = 1000000000; /* (10 ^ 9) */
a[0] = n / m;
for (i = 1; i < 10; i++)
{
m /= 10;
a[i] = (n / m) % 10;
}
for (i = 0, sum = 0, count = 0; i < 10; i++)
{
sum += a[i];
if (sum || i == 9)
{
_putchar('0' + a[i]);
count++;
}
}
return (count);
}
/**
* print_o - takes an unsigned int and prints it in octal notation
* @o: unsigned int to print
*
* Return: number of digits printed
*/
int print_o(va_list o)
{
unsigned int a[11];
unsigned int i, m, n, sum;
int count;
n = va_arg(o, unsigned int);
m = 1073741824; /* (8 ^ 10) */
a[0] = n / m;
for (i = 1; i < 11; i++)
{
m /= 8;
a[i] = (n / m) % 8;
}
for (i = 0, sum = 0, count = 0; i < 11; i++)
{
sum += a[i];
if (sum || i == 10)
{
_putchar('0' + a[i]);
count++;
}
}
return (count);
}
/**
* print_b - takes an unsigned int and prints it in binary notation
* @b: unsigned in to print
*
* Return: number of digits printed
*/
int print_b(va_list b)
{
unsigned int n, m, i, sum;
unsigned int a[32];
int count;
n = va_arg(b, unsigned int);
m = 2147483648; /* (2 ^ 31) */
a[0] = n / m;
for (i = 1; i < 32; i++)
{
m /= 2;
a[i] = (n / m) % 2;
}
for (i = 0, sum = 0, count = 0; i < 32; i++)
{
sum += a[i];
if (sum || i == 31)
{
_putchar('0' + a[i]);
count++;
}
}
return (count);
}