-
Notifications
You must be signed in to change notification settings - Fork 2
/
decimal_to_base_b_conversion.cpp
67 lines (50 loc) · 1.28 KB
/
decimal_to_base_b_conversion.cpp
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
/****************************************************************************
File name: decimal_to_base_b_conversion.cpp
Author: babajr
*****************************************************************************/
/*
Decimal to Base b conversion: (num) base (10) = (num2) base b.
Ex:
(17) base 10 = (10001) base 2
(17) base 10 = (21) base 8
Algo:
--> keep deviding the decimal number with base b until it becomes 0,
and take remainders and write/print it in opposite.
17 % 2 = 1, 17/2 = 8
8 % 2 = 0, 8/2 = 4
4 % 2 = 0, 4/2 = 2
2 % 2 = 0, 2/2 = 1
1 % 2 = 1, 1/2 = 0
Ouput = (10001) base 2
17 % 8 = 1, 17/8 = 2
2 % 8 = 2, 2/8 = 0
Output = (21) base 8.
*/
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
/*
API to convert decimal to binary number.
*/
void decimal_to_base_b(int num, int base)
{
int base_arr[32] = {0};
int i = 0;
while(num > 0)
{
// get the remainder
base_arr[i] = num % base;
num = num / base;
i++;
}
// print the value in base_arr in reverse form.
for(i = 31; i >= 0; i--)
printf("%d ", base_arr[i]);
}
int main(void)
{
int num = 17; // decimal number
int base = 8; // 2 - binary, 8 - octal
decimal_to_base_b(num, base);
return 0;
}