-
Notifications
You must be signed in to change notification settings - Fork 40
/
d_2_b.c
46 lines (46 loc) · 860 Bytes
/
d_2_b.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
// program to convert decimal number into binary and binary into decimal number
#include<stdio.h>
void binary(int n)
{
int rem;
if(n<=1)
{
printf("%d",n);
return;
}
rem=n%2;
binary(n/2);
printf("%d",rem);
}
int main()
{
int n,c,d=0,b=1,r;
printf("1. For decimal to binary\n");
printf("2. for binary to decimal\n");
scanf("%d",&c);
printf("enter number to convert\n");
scanf("%d",&n);
if(c==1)
{
if(n<0)
{
printf("not possible\n");
}
else
{
printf("The decimal -> binary is ");
binary(n);
}
}
else if(c==2)
{
while(n>0)
{
r=n%10;
d=d+(b*r);
n=n/10;
b=b*2;
}
printf("The binary -> decimal is %d\n",d);
}
}