-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_binary.c
More file actions
42 lines (38 loc) · 744 Bytes
/
Copy pathprint_binary.c
File metadata and controls
42 lines (38 loc) · 744 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
36
37
38
39
40
41
42
#include "main.h"
#include <stdarg.h>
/**
* print_bin - prints an integer in binary to the stdout
* @n: integer to be printed
*
* Return: count of characters printed
*/
int print_bin(unsigned int n)
{
unsigned int lastn;
int count = 0;
/*get last digit and remainder as absolute value*/
lastn = n % 2;
n = n / 2;
/*use recursion if remainder value is not zero*/
if (n != 0)
{
count = print_bin(n);
}
_putchar(48 + lastn);
count++;
return (count);
}
/**
* print_binary - prints an integer in binary to the stdout
* @args: va_list object
*
* Return: count of characters printed
*/
int print_binary(va_list args)
{
int count;
unsigned int n;
n = va_arg(args, unsigned int);
count = print_bin(n);
return (count);
}