-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_int.c
90 lines (82 loc) · 1.21 KB
/
print_int.c
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include "main.h"
/**
* print_i -it will print intenger.
* @args:it is an arguument to print.
* Return:it will return an integer if successful.
*/
int print_i(va_list args)
{
int n = va_arg(args, int);
int num, last = n % 10, digit, exp = 1;
int i = 1;
n = n / 10;
num = n;
if (last < 0)
{
_putchar('-');
num = -num;
n = -n;
last = -last;
i++;
}
if (num > 0)
{
while (num / 10 != 0)
{
exp = exp * 10;
num = num / 10;
}
num = n;
while (exp > 0)
{
digit = num / exp;
_putchar(digit + '0');
num = num - (digit * exp);
exp = exp / 10;
i++;
}
}
_putchar(last + '0');
return (i);
}
/**
* print_d -it prints decimal
* @args: it gives argument to print
* Return: integer
*/
int print_d(va_list args)
{
int n = va_arg(args, int);
int num, last = n % 10, digit;
int i = 1;
int exp = 1;
n = n / 10;
num = n;
if (last < 0)
{
_putchar('-');
num = -num;
n = -n;
last = -last;
i++;
}
if (num > 0)
{
while (num / 10 != 0)
{
exp = exp * 10;
num = num / 10;
}
num = n;
while (exp > 0)
{
digit = num / exp;
_putchar(digit + '0');
num = num - (digit * exp);
exp = exp / 10;
i++;
}
}
_putchar(last + '0');
return (i);
}