Home ยป C program to convert decimal number to binary number

C program to convert decimal number to binary number

Below is the code to convert decimal number to binary number

#include <stdio.h>  
int main(){  
    int num,bin_num,dec_num=0,b=1,r;  
    printf("Enter a binary number: ");  
    scanf("%d",&num); 
    bin_num=num;   
     while(num>0){  
        r=num%10;  
        dec_num=r*b+dec_num;  
        num=num/10;   
        b=b*2;  
    }  
    printf("The binary number is %d ",bin_num); 
    printf("\nThe decimal number is %d ",dec_num);  
    return 0;  
}

Contents

Output:

Enter a binary number: 1010100
The binary number is 1010100
The decimal number is 84