-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbtoa.c
More file actions
35 lines (31 loc) · 714 Bytes
/
btoa.c
File metadata and controls
35 lines (31 loc) · 714 Bytes
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
#include "main.h"
/**
* print_binary - convert unsigned int to binary and print
* @arg: va_list argument containing the binary number
* Return: number of printed characters
*/
int print_binary(va_list arg)
{
unsigned int num = va_arg(arg, unsigned int);
int count = 0;
count += dectobin(num);
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);
}