Friday, November 23, 2012

WRITE A C PROGRAM TO CONVERT THE DECIMAL TO BINARY NUMBER


/* WRITE A C PROGRAM TO CONVERT THE FOLLOWING :
            a. DECIMAL TO BINARY NUMBER
            b. BINARY TO DECIMAL NUMBER
            c. REAL DECIMAL NUMBER TO HEXADECIMAL NUMBER
            d. REAL DECIMAL TO OCTAL NUMBER
*/

#include<stdio.h>
#include<conio.h>
#include<ctype.h>  /* isdigit */
#include<stdlib.h> /* atoi */
#include<math.h>
#define MAX 25

void dectobin()
{
            int bin,dec,n,r,k;
            printf("Enter a Decimal number \t");
            scanf("%d",&dec);
            n=dec; bin=0;k=1;
            while(n>0)
            {
                        r=n%2;
                        n=n/2;
                        bin=bin+r*k;
                        k=k*10;
            }
                        printf("\n Decimal = %d \n Binary = %d",dec,bin);
}

void bintodec()
{
            int bin,dec,n,r,k;
            printf("Enter a Binary number \t");
            scanf("%d",&bin);
            n=bin; dec=0;k=1;
            while(n>0)
            {
                        r=n%10;
                        if(r>1)
                        {
                                    printf("\t ERROR!! Not a Binary number");
                                    return;
                        }
                        n=n/10;
                        dec=dec+r*k;
                        k=k*2;
            }
            printf("\n Binary = %d \n Decimal = %d",bin,dec);
}
void rdectohex()
{
            char str[MAX],str1[MAX],str2[MAX],a;
            int i,j,n,r,len,temp,sign=1;

            printf("Enter a Real Decimal Number\t");
            scanf("%s",str);

            for(j=0,i=0 ; str[i]!='.' && str[i]!='\0' ; i++)
            {
                        if(str[i]=='-')
                                    sign=-1;
                        else if(str[i]=='+')
                                    sign=1;
                        else if(str[i]>='0' && str[i]<='9')
                                    str1[j++]=str[i];
                        else
                        {
                                    printf("ERROR!! Not a Real Decimal Number");
                                    return;
                        }
            }
            str1[j]='\0';

            i++;

            for(j=0 ; str[i]!='\0' ; i++)
            {
                        if(str[i]>='0' && str[i]<='9')
                                    str2[j++]=str[i];
                        else
                        {
                                    printf("ERROR!! Not a Real Decimal Number");
                                    return;
                        }
            }
            str2[j]='\0';

            /* before decimal */
            i=0;
            n=atoi(str1);
            while(n>0)
            {
                        r=n%16;
                        if(r>9)
                        {
                                    switch(r)
                                    {
                                                case 10:a='A';break;
                                                case 11:a='B';break;
                                                case 12:a='C';break;
                                                case 13:a='D';break;
                                                case 14:a='E';break;
                                                case 15:a='F';break;
                                    }
                        }
                        else
                                    a=(char)r+'0';
                        n=n/16;
                        str1[i++]=a;
            }
            str1[i]='\0';
            /* after decimal */
            i=0;
            j=0;
            n=atoi(str2);
            len=strlen(str2);
            while(j<5)
            {
                        temp=pow(10,len);

                        r=(n*16) / temp; /* r=quotient */
                        n=(n*16) % temp;

                        if(r>9)
                        {
                                    switch(r)
                                    {
                                                case 10:a='A';break;
                                                case 11:a='B';break;
                                                case 12:a='C';break;
                                                case 13:a='D';break;
                                                case 14:a='E';break;
                                                case 15:a='F';break;
                                    }
                        }
                        else
                                    a=(char)r+'0';
                        str2[i++]=a;
                        j++;
            }
            str2[i]='\0';

            /* To Display */
            strrev(str1);
            if(sign==-1)
                        printf("\nHexadecimal : -%s.%s",str1,str2);
            else
                        printf("\nHexadecimal : %s.%s",str1,str2);
}
void rdectooct()
{
            char str[MAX],str1[MAX],str2[MAX];
            int i,j,n,r,k,temp,len,sign=1,oct1,oct2;

            printf("Enter a Real Decimal Number\t");
            scanf("%s",str);

            for(j=0,i=0 ; str[i]!='.' && str[i]!='\0' ; i++)
            {
                        if(str[i]=='-')
                                    sign=-1;
                        else if(str[i]=='+')
                                    sign=1;
                        else if(str[i]>='0' && str[i]<='9')
                                    str1[j++]=str[i];
                        else
                        {
                                    printf("ERROR!! Not a Real Decimal Number");
                                    return;
                        }
            }
            str1[j]='\0';

            i++;

            for(j=0 ; str[i]!='\0' ; i++)
            {
                        if(str[i]>='0' && str[i]<='9')
                                    str2[j++]=str[i];
                        else
                        {
                                    printf("ERROR!! Not a Real Decimal Number");
                                    return;
                        }
            }
            str2[j]='\0';

            /* before decimal */
            i=0;
            n=atoi(str1);
            oct1=0;k=1;
            while(n>0)
            {
                        r=n%8;
                        if(r>7)
                        {
                                    printf("\t ERROR!! Not a Octal number");
                                    return;
                        }
                        n=n/8;
                        oct1=oct1+r*k;
                        k=k*10;
            }

            /* After decimal */
            oct2=0;
            j=0;
            n=atoi(str2);
            len=strlen(str2);
            temp=pow(10,len);
            while(j<4)
            {
                        r=(n*8)/temp; /* r is Quotient and generated
                                                                                     carry is indicated by it*/
                        n=(n*8)%temp;
                        oct2=(oct2*10)+r;
                        j++;
            }

            /* To Display */
            if(sign==-1)
                        printf("\nOctal : -%d.%d",oct1,oct2);
            else
                        printf("\nOctal : %d.%d",oct1,oct2);
}
void main()
{
            int flag=1,choice=0;
            while(flag!=0)
            {
                        clrscr();
                        printf("1. Decimal to Binary\n");
                        printf("2. Binary to Decimal\n");
                        printf("3. Real Decimal to Hexadecimal\n");
                        printf("4. Real Decimal to Octal\n");
                        printf("5. Exit\n");
                        printf("Enter choice -->\t");
                        scanf("%d",&choice);
                        switch(choice)
                        {
                                    case 1: dectobin();flag=1;getch();break;
                                    case 2: bintodec();flag=1;getch();break;
                                    case 3: rdectohex();flag=1;getch();break;
                                    case 4: rdectooct();flag=1;getch();break;
                                    default: flag=0;break;
                        }
                        choice=0;
            }
}
/*
            =====OUT PUT=====
           
            1. Decimal to Binary
            2. Binary to Decimal
            3. Real Decimal to Hexadecimal
            4. Real Decimal to Octal
            5. Exit
            Enter choice -->        1
            Enter a Decimal number  10

             Decimal = 10
             Binary = 1010

            1. Decimal to Binary
            2. Binary to Decimal
            3. Real Decimal to Hexadecimal
            4. Real Decimal to Octal
            5. Exit
            Enter choice -->        2
            Enter a Binary number   1010
           
             Binary = 1010
             Decimal = 10

            1. Decimal to Binary
            2. Binary to Decimal
            3. Real Decimal to Hexadecimal
            4. Real Decimal to Octal
            5. Exit
            Enter choice -->        3
            Enter a Real Decimal Number     12.87

            Hexadecimal : C.DEB85

            1. Decimal to Binary
            2. Binary to Decimal
            3. Real Decimal to Hexadecimal
            4. Real Decimal to Octal
            5. Exit
            Enter choice -->        4
            Enter a Real Decimal Number     25.895

            Octal : 31.7121

*/

0 comments:

Post a Comment