-
Notifications
You must be signed in to change notification settings - Fork 2
/
task_1.c
82 lines (78 loc) · 1.28 KB
/
task_1.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
#include "holberton.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
/**
* print_i - prints an integer
* @i: integer to print
* Description: print number between -2,147,483,648 and 2,147,483,647
* Return: number of digits printed and if negative adds the sign printed
*/
int print_i(va_list i)
{
int a[10];
int it, m, n, sum, j;
n = va_arg(i, int);
j = 0;
m = 1000000000;
a[0] = n / m;
for (it = 1; it < 10; it++)
{
m /= 10;
a[it] = (n / m) % 10;
}
if (n < 0)
{
_putchar('-');
j++;
for (it = 0; it < 10; it++)
a[it] *= -1;
}
for (it = 0, sum = 0; it < 10; it++)
{
sum += a[it];
if (sum != 0 || it == 9)
{
_putchar('0' + a[it]);
j++;
}
}
return (j);
}
/**
* print_d - prints a decimal
* @d: decimal to print
* Description: print a base 10 number
* Return: number of digits printed and if negative adds the sign printed
*/
int print_d(va_list d)
{
int a[10];
int it, m, n, sum, j;
n = va_arg(d, int);
j = 0;
m = 1000000000;
a[0] = n / m;
for (it = 1; it < 10; it++)
{
m /= 10;
a[it] = (n / m) % 10;
}
if (n < 0)
{
_putchar('-');
j++;
for (it = 0; it < 10; it++)
a[it] *= -1;
}
for (it = 0, sum = 0; it < 10; it++)
{
sum += a[it];
if (sum != 0 || it == 9)
{
_putchar('0' + a[it]);
j++;
}
}
return (j);
}