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

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including 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. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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