Binary number to Decimal number and vice versa in C language.

 WAP to convert binary number into decimal number and vice versa.


#include<stdio.h>

#include<conio.h>

#inlude<math.h>

void main()

{

  int n,dnum,bnum,i,rem,t;

  clrscr();

  printf("'\n  Press 1 to convert Decimal number to Binary number");

  printf("\n  Press 2 to convert Binary number to Decimal number");

  scanf("%d",&n);

  if(n==1)

  {

    printf("\n Enter Decimal number :");

    scanf("%d",&dnum);

    t=dnum;

    i=1;

    bnum=0;

    while(t>0)

    {

      rem=t%2;

      bnum=bnum+rem*i;

      t=t/2;

      i=i*10;

    }

    printf("\n Decimal number :%d",dnum);

    printf("\n Binary number  :%d",bnum);

    getch();

  }

  if(n==2)

  {

   printf("\n Enter Binary number:");

   scanf("%d",&bnum);

   t=bnum;

   i=0;

   dnum=0;

   while(t>0)

   {

    rem=t%10;

    dnum=dnum+rem*pow(2,i);

     t=t/10;

    i++;

   }

   printf("\n Binay number    :%d",bnum);

   printf("\n Decimal number  :%d",dnum);

   getch();


  }

 }

Output:-1-

Press 1 to convert Decimal number to Binary number

Press 2 to convert Binary number to Decimal number

1

Enter Decimal number

15

Decimal number = 15

Binary number  =  1111


Output:-2-

Press 1 to convert Decimal number to Binary number

Press 2 to convert Binary number to Decimal number

2

Enter Decimal number

1100

Binary number  =  1100

Decimal number=  12


Post a Comment

0 Comments