forked from Siddhesh-3/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_decimal_conversion.c
More file actions
43 lines (36 loc) · 835 Bytes
/
Copy pathbinary_decimal_conversion.c
File metadata and controls
43 lines (36 loc) · 835 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
//Program to convert binary number to decimal and vice versa
#include<stdio.h>
#include<math.h>
int DecimaltoBinary(int);
int binarytodecimal(int);
int main(){
int number,binary,num,decimal;
printf("Enter the decimal number");
scanf("%d",&number);
printf("Enter the binary number");
scanf("%d",&num);
binary= DecimaltoBinary(number);
decimal=binarytodecimal(num);
printf("Decimal to Binary conversion of %d is %d\n",number,binary);
printf("Binary to decimal conversion of %d is %d\n",num,decimal);
}
int DecimaltoBinary(int number){
int binary=0,i=1,remainder;
while(number!=0){
remainder=number%2;
number/=2;
binary =binary+remainder*i;
i*=10;
}
return binary;
}
int binarytodecimal(int num){
int lastdigit,decimal=0,i=0;
while(num!=0){
lastdigit=num%10;
num/=10;
decimal = decimal+lastdigit*pow(2,i);
i++;
}
return decimal;
}