Conversion Decimal Number into Binary Number and Vice Varsa

Desimal to banary number 

#include<stdio.h>
#include<conio.h>
 
int main(){
 
long int num,r,p=10;
 
clrscr();
 
printf("Enter  any  number: ");
scanf("%ld",num);
printf("The binary equivalent is\n: ");
While(num !=0)
{
     r=num%2;
     gotoxy(p,4) ;
  printf("%d",r);
     num/=2;
     p=p-1;
}
getch();

 

 
 

Output

Enter any number any number: 15
Equivalent decimal value: 1111
 
Banary to Desimal number


 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h> 
#include<math.h> 
 
int main(){
int num,r,p=0,sum=0;
clrscr();
printf("Enter any number : ");
scanf("%d",num);
While(num !=0)
{
    r=num%10;
    if(r!=0 && r!=1)
   {
        printf("not a binary number\n");
        getch();
        exit(0);
  }
  sum = sum+r*(int) pow(2,p);
  num / = 10;
  p++;
}
 printf("The decimal number is %d\n : ",sum);
Getch();
 
 
Ebook Download
View all
Learn
View all