C Program to convert decimal number to binary number

Simple C program to convert decimal number to binary number: Decimal number is base-10 number and binary number is a base-2 number. In binary number, there are only two number 0 and 1 and in decimal numbers, there are 0 to 9 numbers.
Number 435 is a decimal number and 1100 is a binary number.
 
/* C program to convert decimal number to binary number */
#include<stdio.h>
#include<math.h>
int main()
{
 int dec,bin=0,i=0;
 printf("Enter a decimal number: ");
 scanf("%d",&dec);
 while(dec!=0)
 {
  int rem=dec%2;
  bin=bin+rem*pow(10,i);
  dec=dec/2;
  i++;
 }
 printf("Binary number = %d",bin);
 return 0;

/* End of the program */
 

Another C program to convert decimal number to binary number using bits
 
/* C program to convert decimal number to binary number */
#include<stdio.h>
#include<math.h>
int main()
{
 int dec,bin,i=0;
 printf("Enter a decimal number: ");
 scanf("%d",&dec);
 printf("Binary number \n");
 for(i=31;i>=0;i--)
 {
  bin=dec>>i;
  if(bin&1)
  printf("1");
  else
  printf("0");
 }
 return 0;
}
/* End of the program */
 
C program to convert decimal number to binary number using array
 
/* C program to convert decimal number to binary number */
#include<stdio.h>
#include<math.h>
int main()
{
 int dec,bin,i=0,j;
 int arr[32];
 printf("Enter a decimal number: ");
 scanf("%d",&dec);
 printf("Binary number = ");
 for(i=0;dec!=0;i++)
 {
   arr[i]=dec%2;
   dec=dec/2; 
 }
 for(j=i-1;j>=0;j--)
 {
  printf("%d",arr[j]);
 }
 return 0;
}
/* End of the program */

Popular posts from this blog