C program to convert decimal to binary number

C program to convert decimal to binary number

This page has a C program to convert decimal to binary number. We can write the C program to convert the given mixed decimal number into its equivalent binary. For Integer part conversiondivide the given integer part of the decimal number by 2 again and again until the number can be divided by 2 and then print the remainders of each division form bottom to top to form the binary equivalent. For decimal part conversion, multiply the given decimal part (number after the decimal point) of the decimal number by 2 again and again for atleast 5 or 7 steps(times) and then print the carries of each multiplication form top to bottom to form the binary equivalent.  The below given C program to convert a given mixed decimal number into its equivalent binary numberKindly check out the program to convert decimal to binary number.

Convert decimal to binary


   #include<stdio.h>
   #include<string.h>
   void decimal1_to_binary(long int);
   void decimal2_to_binary(long int,int);
   int power(int,int);
   int main(){ int a=0,j=0,m,n=0,q=1;
           long int k1,k2;
           char g1[80], p1[80]={'\0'}, p2[80]={'\0'};
           clrscr();
           printf("Give a decimal number : ");
           gets(g1);
           for (a = 0; g1[a] != '\0'; a++)
           {
           q++;
           }
           for(m=0;m<q-1;m++){
           if(g1[m] == '.')
           break;
           p1[m]=g1[m];
          }
          for(a=m+1;a<=q-1;a++){
          p2[n]=g1[a];
          n++;
          j++;
         }
         k1= atoi(p1);
         k2= atoi(p2);
         printf("\n\n The given decimal number is : %s\n\n", g1);
         decimal1_to_binary(k1);
         for(m=0;m<q-1;m++){
         if(g1[m] == '.'){
         printf(".");
         break;
         }
         }
         if(g1[m] == '.')
        decimal2_to_binary(k2, j-1);
        getch();
        return(0);
        }

   void decimal1_to_binary(long int s){
        int b=1, h, remainde[40];
        printf("\n\n The Binary number is : ");
        while(s>0){
        remainde[b] = s % 2;
        s = s / 2;
        b++;
        }
        b--;
        for(h=b;h>=1;h--)
        printf("%d", remainde[h]);
        return(0);
        }

   void decimal2_to_binary(long int e, int u){
         int a,p,r;
         for(a=1;a<=8;++a){
         e=e*2;
         p=power(10,u);
         if(e>=p){
         r = e / p;
         e = e % p;
         printf("%d",r);
         }
         else
         printf("%d",0);
         }
         return(0);
         }

   int power(int x,int n){
        int i;
        int numb = 1;
        for (i = 0; i < n; ++i)
        numb *= x;
        return(numb);
        }


Sample Output:

Enter an Integer decimal number : 13

The given decimal number is : 13

The Binary number is : 1101

 


About Er. T.Tharesh Kumar

I am a Professor and Head of Department of Computer Science in an Arts and Science College. I have designed this website to provide solved example programs of different categories for all the school and college going students to learn C language deeply, and to practice Interview based Multiple Choice (Objective Type) questions and answers.

Other Related C Programs :


* C Program to find whether the given number is Odd or Even

* C Program to find the Sum of Odd Digits

* C Program to find the Sum of Even Digits

* C Program to find the Sum of Even and Odd Digits,

* C Program to Display Odd digits

* C Program to Display even digits

* C Program to Display Odd Numbers,

* C Program to Display Even Numbers,

* C Program to Check whether the given number is Armstrong number

* C Program to display the Armstrong numbers in Range

* C Program to display the Armstrong numbers between 1 and 1000

* C Program to check perfect number, perfect numbers in range

* C Program to Check Prime number, Prime numbers between 1 and 100

* C Program to display the Prime numbers in a Range

* C Program to display the Sum of Prime numbers

* C Program to Check Strong number

* C Program to display the Strong number in Range

* C Program to Check whether the given number is polindrome number or not

* C Program to Check polindrome number using for loop

* C Program to Check polindrome number using Recursion

* C Program to  check whether the given string is polindrome or not

* C Program to find the Roots of Quadratic equation

* C Program to display Fibonacci Series

* C Program to display the Fibonacci Series using an array

* C Program to display the Fibonacci Series using while loop

* C Program to find the Sum of Fibonacci Series, Factorial of Number

* C Program to find the Factorial without using Recursion

* C Program to find the Factorial using Pointers

* C Program to find the Factorial using Function

Bookmark and Share