Sunday, 1 September 2013

Write a c program to convert decimal number to binary number.



Write a c program to convert decimal number to binary number.

Binary number system: It is base 2 number system which uses the digits from 0 and 1.
Decimal number system: It is base 10 number system which uses the digits from 0 to 9

#include<stdio.h>
int main(){

    long int decimalNumber,remainder,quotient;
    int binaryNumber[100],i=1,j;

    printf("Enter any decimal number: ");
    scanf("%ld",&decimalNumber);

    quotient = decimalNumber;

    while(quotient!=0){
         binaryNumber[i++]= quotient % 2;
         quotient = quotient / 2;
    }

    printf("Equivalent binary value of decimal number %d: ",decimalNumber);
    for(j = i -1 ;j> 0;j--)
         printf("%d",binaryNumber[j]);

    return 0;
}

Sample output:
Enter any decimal number: 50
Equivalent binary value of decimal number 50: 110010

0 comments:

Post a Comment

 

C Programming Language Interview Questions and Answers Tutorial for beginners. Copyright 2013 All Rights Reserved