Monday, February 15, 2016

C Program to convert a decimal Number into binary

/*=================================================================================
 **
 **  File Name     : decToBin.c
 **  Creation Date : Mon 15 Feb 2016 10:10:36 PM IST
 **  Last Modified : Mon 15 Feb 2016 10:24:09 PM IST
 **  Compoler      : gcc
 **  Author        : Manoj Kumar Patra, manojpatra.sit@gmail.com
 **  Organization  : SCIS, University of Hyderabad, India.
 **
 **===============================================================================*/

#include <stdio.h>
#include <math.h>
int main()
{
    printf("\n= = = = = = = Decimal to Binary = = = = = = = = =\n");
    int n, bin=0, rem, i=1, k;
    printf("\nEnter a decimal number : ");
    scanf("%d", &n);
    k=n;
    while (n!=0)
    {
        rem=n%2;
        n/=2;
        bin+=rem*i;
        i*=10;
    }
    printf("\nThe binary equivalent of %d is = %d\n",k,bin);
    printf("\n= = = = = = = = = = = = = = = = = = = = = = = = =\n\n");
}

OutPut :

max@ubuntu:~/cprog$ gcc decToBin.c -o decToBin
max@ubuntu:~/cprog$ ./decToBin

= = = = = = = Decimal to Binary = = = = = = = = =

Enter a decimal number : 100

The binary equivalent of 100 is = 1100100

= = = = = = = = = = = = = = = = = = = = = = = = =

max@ubuntu:~/cprog$ ./decToBin

= = = = = = = Decimal to Binary = = = = = = = = =

Enter a decimal number : 15

The binary equivalent of 15 is = 1111

= = = = = = = = = = = = = = = = = = = = = = = = =

No comments:

Post a Comment