-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumb_func.c
More file actions
75 lines (59 loc) · 1.01 KB
/
numb_func.c
File metadata and controls
75 lines (59 loc) · 1.01 KB
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
#include "holberton.h"
#include <stdarg.h>
#include <stdio.h>
/**
* print_int - Outputs interpolated argument from @ap
*
* @ap: Variadic list of arguments to be processed
*
* Return: Length of processed data
*/
int print_int(va_list ap)
{
int divisor = 1, length = 0;
unsigned int c = 0;
int value = va_arg(ap, int);
if (value < 0)
{
_putchar('-');
c = value * -1;
length++;
}
else
{
c = value;
}
while (c / divisor > 9)
{
divisor *= 10;
}
while (divisor > 0)
{
_putchar((c / divisor) + '0');
c %= divisor;
divisor /= 10;
length++;
}
return (length);
}
int convert(unsigned int value, unsigned int base)
{
unsigned int answer = value % base;
int depth = 1;
value = value / base;
if (value > 0)
{
depth = convert(value, base);
}
_putchar(answer + '0');
return (1 + depth);
}
int print_bin(va_list ap)
{
unsigned int value = va_arg(ap, int);
unsigned int base = 2;
int length = 0;
length = convert(value, base);
length -= 2;
return (length);
}