-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathoctal2binary.c
More file actions
54 lines (42 loc) · 728 Bytes
/
octal2binary.c
File metadata and controls
54 lines (42 loc) · 728 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
43
44
45
46
47
48
49
50
51
52
53
54
// # Write a program to convert Octal to Binary
/*
Examples:
Input : 25
Output : 10101
Input : 51
Output : 101001
Input: 5
Output: 101
*/
#include <stdio.h>
int octal2bin(int octal)
{
int dec = 0, base = 1;
while (octal != 0)
{
dec += (octal % 10) * base;
base *= 8;
octal /= 10;
}
int bin = 0;
base = 1;
while (dec != 0)
{
bin += ((dec % 2) * base);
base *= 10;
dec /= 2;
}
return bin;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
int num;
scanf("%d", &num);
printf("%d\n", octal2bin(num));
}
return 0;
}