From 0d33592e5816091a2e741fc8f5a579d0f1c457d6 Mon Sep 17 00:00:00 2001 From: abcdefgh12 <43812657+abcdefgh12@users.noreply.github.com> Date: Wed, 3 Oct 2018 17:00:56 +0530 Subject: [PATCH] btod.c --- btod.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 btod.c diff --git a/btod.c b/btod.c new file mode 100644 index 0000000..d9b8ec2 --- /dev/null +++ b/btod.c @@ -0,0 +1,91 @@ +// program to convert decimal number into binary and binary into decimal number + +#include + +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); + + } + +}