×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to convert number from Decimal to Binary

In this program, we will read an integer number in Decimal and converts it into Binary Number System. This program is for Decimal to Binary Conversion in C.

Converting number from Decimal to Binary

The logic behind to implement this program - Get remainder using modulus operator by 2 and store it into an array then divide number by 2, repeat this process till given number is greater than 0. Because 2 is the base of Binary Number System.

For more details Learn: Computer Number System and its conversions.

Decimal to Binary Conversion using C program

/*C program to convert number from decimal to binary*/
 
#include <stdio.h>
 
int main()
{
    int     number,cnt,i;
    int     bin[32];
 
    printf("Enter decimal number: ");
    scanf("%d",&number);
 
    cnt=0;              /*initialize index to zero*/
    while(number>0)
    {
        bin[cnt]=number%2;
        number=number/2;
        cnt++;
    }
 
    /*print value in reverse order*/
    printf("Binary value is: ");
    for(i=(cnt-1); i>=0;i--)
        printf("%d",bin[i]);
 
    return 0;
}

Output

Enter decimal number: 545
Binary value is: 1000100001

C Number System Conversion Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.