C Program to Convert Decimal to Binary Number

C Program to Convert Decimal to Binary Number

C program to convert decimal to binary; Through this tutorial, we will learn how to convert decimal numbers to binary numbers in c program using for loop, while loop, function, and recursion.

Programs to Convert Decimal to Binary Number in C

  • C Program to Convert Decimal to Binary Number using While Loop
  • C Program to Convert Decimal to Binary Number using For Loop
  • C Program to Convert Decimal to Binary Number using Function
  • C Program to Convert Decimal to Binary Number using Recursion

C Program to Convert Decimal to Binary Number using While Loop

/* C Program to Convert Decimal to Binary */
 
#include <stdio.h>
 
int main() 
{
    int a[10], number, i = 1, j;
    printf("\n Please Enter Decimal Number  :  ");
    scanf("%d", &number);
    
    while(number  != 0)
    {
        a[i++] = number % 2;
        number = number / 2;
    }
    
    printf("\n Binary Number of a Given Number =  ");
    for(j = i - 1; j > 0; j--)  {
        printf(" %d ", a[j]);
    }
    return 0;
}

The output of the above c program; as follows:

Please Enter Decimal Number  :  220
Binary Number of a Given Number =   1  1  0  1  1  1  0  0 

C Program to Convert Decimal to Binary Number using For Loop

/* C Program to Convert Decimal to Binary */
 
#include <stdio.h>
int main() 
{
    int a[10], number, i, j;
    printf("\n Please Enter decimal number  :  ");
    scanf("%d", &number);
    
    for(i = 0; number > 0; i++)
    {
        a[i] = number % 2;
        number = number / 2;
    }
    
    printf("\n Binary Number of a Given Number =  ");
    for(j = i - 1; j >= 0; j--)  {
        printf(" %d ", a[j]);
    }
    printf("\n");
    return 0;
}

The output of the above c program; as follows:

Please Enter decimal number  :  100
Binary Number of a Given Number =   1  1  0  0  1  0  0 

C Program to Convert Decimal to Binary Number using Function

#include <stdio.h>
#include <math.h>

long decimalToBinary(int decimalnum)
{
    long binarynum = 0;
    int rem, temp = 1;

    while (decimalnum!=0)
    {
        rem = decimalnum%2;
        decimalnum = decimalnum / 2;
        binarynum = binarynum + rem*temp;
        temp = temp * 10;
    }
    return binarynum;
}

int main()
{
    int decimalnum;
    printf("Enter a Decimal Number: ");
    scanf("%d", &decimalnum);
    printf("Equivalent Binary Number is: %ld", decimalToBinary(decimalnum));
    return 0;
}

The output of the above c program; as follows:

Enter a Decimal Number: 100
Equivalent Binary Number is: 1100100

C Program to Convert Decimal to Binary Number using Recursion

#include <stdio.h>
 
int decimal_binary(int n)
{
    if (n==0)
        return 0;
    else
        return ((n%2)+10*decimal_binary(n/2));
}
 
void main()
{
   int no;
 
   printf("Enter a decimal number : ");
   scanf("%d",&no);
   printf("Decimal(%d) = Binary(%d)\n",no,decimal_binary(no));
}

The output of the above c program; as follows:

Enter a decimal number : 100
Decimal(100) = Binary(1100100)

Recommended C Programs

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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