Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Program For Decimal To Binary Conversion (http://www.go4expert.com/articles/program-decimal-binary-conversion-t2630/)

bharathidhas 21Jan2007 06:48

Program For Decimal To Binary Conversion
 
Code for Decimal To Binary Conversion
Code: C

#include<stdio.h>
void main()
{
    int i,n1,t,w,a[100],i1=0;
    float n,f2;
   
    clrscr();
    printf("[B]PROGRAM FOR DECIMAL TO BINARY CONVERSION[/B]\n");
    printf("******************************************\n");
    printf("\n\nENTER A NUMBER:");
    scanf("%f",&n);
    w=n;
    f2=w;
   
    if(f2==n)
    {
        printf("");f2=0;
    }
    else
    {
        f2=n-f2;
    }
    n1=n;
   
    for(i=0;n1>=1;i++)
    {
        t=n1%2;
        n1=n1/2;
        i1++;
        a[i]=t;
    }
   
    printf("\n\nBINARY EQVALENT=>");
   
    for(i=(i1-1);i>=0;i--)
    {
        printf("%d",a[i]);
    }
    printf(".");
   
    for(i=1;i<=6;i++)
    {
        f2=f2*2;
        if(f2>=1)
        {
            printf("1");
            f2=f2-1;
        }
        else
        {     printf("0");
        }
    }
    getch();
}


shabbir 21Jan2007 10:25

Re: Program For Decimal To Binary Conversion
 
We already have a similar program in the following article Decimal, Hex, octal and binary number inter conversion. The program is Decimal to binary.

supriya 29Jun2007 16:18

hex to octal conversion
 
Hi,
I am new member of this site. I am not getting how to convert Hexadecimal number into octal no. AND vice-versa.

Please give me code.

Thanks.

SUPRYA

seeguna 30Jun2007 15:50

Re: hex to octal conversion
 
Supriya,
Shabbir already provide a good article for number conversion.Go thru it for all type of number conversions.
For your reference i make a slight touch to existing (Shabbir) program.
To convert Hexadecimal no to octal no......(I just combined two functions into a single one)

Coding listed below:
//first convert Hex to Dec number
//Next convert Dec to Oct number

Code: CPP

#include<stdio.h>
#include<string.h>
#include<math.h>
void main()
{
    char hex[100];
    double dec=0.0;
    int oct[100],temp[100];
    int len,i,j,num=0;
   
    printf("Enter a hexadecimal number");
    scanf("%s",hex);
    len=strlen(hex);
   
    for(i=0;i<len;i++)
    {
        switch(hex[i])
        {
        case '0':
            temp[i]=hex[i]-48;     //Ascii code of 0 is 48  48-48=0//
            break;
        case '1':
            temp[i]=hex[i]-48;    //Ascii code of 1 is 49  49-48=1//
            break;
        case '2':
            temp[i]=hex[i]-48;    //Ascii code of 2 is 50  50-48=2//
            break;
        case '3':
            temp[i]=hex[i]-48;    //Ascii code of 3 is 51  51-48=3//
            break;
        case '4':
            temp[i]=hex[i]-48;    //Ascii code of 4 is 52  52-48=4//
            break;
        case '5':
            temp[i]=hex[i]-48;    //Ascii code of 5 is 53  53-48=5//
            break;
        case '6':
            temp[i]=hex[i]-48;    //Ascii code of 6 is 54  54-48=6//
            break;
        case '7':
            temp[i]=hex[i]-48;    //Ascii code of 7 is 55  55-48=7//
            break;
        case '8':
            temp[i]=hex[i]-48;    //Ascii code of 8 is 56  56-48=8//
            break;
        case '9':
            temp[i]=hex[i]-48;    //Ascii code of 9 is 57  57-48=9//
            break;
        case 'A':
            temp[i]=hex[i]-55;     //Ascii code of A is 65 65-55=10//
            break;
        case 'B':
            temp[i]=hex[i]-55;    //Ascii code of B is 65 66-55=11//
            break;
        case 'C':
            temp[i]=hex[i]-55;    //Ascii code of C is 65 67-55=12//
            break;
        case 'D':
            temp[i]=hex[i]-55;    //Ascii code of D is 65 68-55=13//
            break;
        case 'E':
            temp[i]=hex[i]-55;    //Ascii code of E is 65 68-55=14//
            break;
        case 'F':
            temp[i]=hex[i]-55;    //Ascii code of F is 65 69-55=15//
            break;
        }
    }
    for(i=0,j=len;i<len;i++,j--)
        dec=dec+(temp[j-1]*pow(16,i));
    printf("The decimal equivalent is %lg \n",dec);
    num=(int)dec;
    //OCTAL CONVERSION
    for(i=0;num!=0;i++)
    {
        oct[i]=num%8;
        num=num/8;
    }
    i--;
    printf("\n Octal equivalent number:");
    for(;i>=0;i--)
        printf("%d",oct[i]);
    printf("\n");

}


seiteki_cai 31Jul2007 17:09

Re: Program For Decimal To Binary Conversion
 
do you know how to convert general tree to binary tree and vice versa?

shabbir 31Jul2007 18:44

Re: Program For Decimal To Binary Conversion
 
Quote:

Originally Posted by seiteki_cai
do you know how to convert general tree to binary tree and vice versa?

This article is about Number conversion and if you have some specific query create a new thread with proper title in the right forum.


All times are GMT +5.5. The time now is 08:18.