-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumbers
More file actions
76 lines (67 loc) · 1.62 KB
/
numbers
File metadata and controls
76 lines (67 loc) · 1.62 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 "main.h"
/**
* print_number - a function that prints an integer
* @n: the number to be printed
* @width: field width
* @precision: precision
* Return: no return
*/
int print_number(va_list n, int width, int precision)
{
int num = va_arg(n, int), count = 0;
count += numprint(num);
return (print_width(NULL, width, count) + print_precision(NULL, precision, count));
}
/**
* numprint - a function that prints a number using recursion
* @arg: the number to be printed
* Return: number of digits printed
*/
int numprint(int arg)
{
unsigned int k = arg;
int count = 0;
if (arg < 0)
{
_putchar('-');
count++;
arg *= -1;
k = arg;
}
k /= 10;
if (k)
count += numprint(k);
count += _putchar(((unsigned int)arg % 10) + '0');
return (count);
}
/**
* dectobin - prints a decimal number in binary
* @num: number to print
* Return: Number of characters printed
*/
int dectobin(unsigned int num)
{
unsigned int remainder, quotient;
int count = 0;
if (!(num))
return (_putchar('0'));
quotient = num / 2;
remainder = num % 2;
if (quotient)
count += dectobin(quotient);
count += _putchar((remainder) + '0');
return (count);
}
/**
* printbin - print a number in binary
* @arg: va_list variable passed
* @width: field width
* @precision: precision
* Return: count of binary digits
*/
int printbin(va_list arg, int width, int precision)
{
int num = va_arg(arg, unsigned int), count = 0;
count += dectobin(num);
return (print_width(NULL, width, count) + print_precision(NULL, precision, count));
}