-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_putnbr_fd.c
More file actions
55 lines (49 loc) · 1.46 KB
/
ft_putnbr_fd.c
File metadata and controls
55 lines (49 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putnbr_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fulloa-s <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/18 10:24:11 by fulloa-s #+# #+# */
/* Updated: 2021/01/18 12:45:39 by fulloa-s ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int decimal(int n)
{
int digit;
digit = 1;
while (n > 9 || n < -9)
{
n /= 10;
digit++;
}
return (digit);
}
int ft_recursive_power(int nb, int power)
{
if (power == 0)
return (1);
return (nb * ft_recursive_power(nb, power - 1));
}
void ft_putnbr_fd(int n, int fd)
{
int digit;
if (n == -2147483648)
{
ft_putstr_fd("-2147483648", fd);
return ;
}
digit = decimal(n);
if (n < 0)
{
ft_putchar_fd('-', fd);
n *= -1;
}
while (digit-- > 0)
{
ft_putchar_fd((n / (ft_recursive_power(10, digit))) + 48, fd);
n %= ft_recursive_power(10, digit);
}
}