Convert Decimal to Binary in C

Get Certified in C Programming and Take Your Skills to the Next Level

Program 1

// Program for Binary to Decimal
#include<stdio.h>
#include<conio.h>
int main()
{
    system("cls");
       int n,dn=0,r,b=1,binary;
       printf("Enter a binary no(only o and 1 format)");
       scanf("%d",&n);  // n= 1011
       binary=n;   //binary=1011  
       while(n!=0) // 1 !=0  
       {
          r=n%10;    // r=1
          dn=dn+r*b;  // dn=11
          b=b*2;     // b =16
          n=n/10;  //0
       }
       printf("\n Binary Number: %d",binary);
       printf("\n Decimal Number: %d",dn);
    return 0;
}

 

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *