C Programming Tutorial

 
 
 
 
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<math.h>
void main()
{
    int a[20],b[20],c[20],rev[20];
    int i,j,k,l,flag,cntint,cntfra,rem,num1,num3;
    char s[20];
    float rem1,num2,num4;
    clrscr();
    rem1=num2=num4=0.0;
    i=j=k=flag=cntint=cntfra=rem=num1=num3=0;
    printf(\"ENTER THE HEXADECIMAL VALUE : \");
    scanf(\"%s\",s);
    for(i=0;i<strlen(s) ;i++)
    {
        if(s[i]==\'.\')
        {
            flag=1;
        }
        else if(flag==0)
        {
            if(s[i]==\'A\')
            {
                a[j++]=10;
            }
            else if(s[i]==\'B\')
            {
                a[j++]=11;
            }
            else if(s[i]==\'C\')
            {
                a[j++]=12;
            }
            else if(s[i]==\'D\')
            {
                a[j++]=13;
            }
            else if(s[i]==\'E\')
            {
                a[j++]=14;
            }
            else if(s[i]==\'F\')
            {
                a[j++]=15;
            }
            else
            {
                a[j++]=s[i]-48;
            }
        }
        else if(flag==1)
        {
            if(s[i]==\'A\')
            {
                b[k++]=10;
            }
            else if(s[i]==\'B\')
            {
                b[k++]=11;
            }
            else if(s[i]==\'C\')
            {
                b[k++]=12;
            }
            else if(s[i]==\'D\')
            {
                b[k++]=13;
            }
            else if(s[i]==\'E\')
            {
                b[k++]=14;
            }
            else if(s[i]==\'F\')
            {
                b[k++]=15;
            }
            else
            {
                b[k++]=s[i]-48;
            }
        }
    }
    cntint = j;
    cntfra = k;
    for(j=0,i=cntint-1;i>=0;i--,j++)
    {
        rem = rem + (a[j] * pow(16,i));
    }
    for(j=0,i=1;j<cntfra;j++,i++)
    {
        rem1 = rem1 + (b[j] / pow(16,i));
    }
    rem1 = rem1 + rem;
    num1 = (int)rem1;
    num2 = rem1 -num1;
    i=0;
    rem = 0;
    while(num1!=0)
    {
        rem = num1 % 2;
        rev[i] = rem;
        num1 = num1 / 2;
        i++;
    }
    j=0;
    while(num2!=0.0)
    {
        num2 =num2 * 2;
        num3 = (int) num2;
        num4 = num2 - num3;
        num2 = num4;
        a[j] = num3;
        j++;
        if(j==4)
        {
            break;
        }
    }
    l=i;
    printf (\"\\nTHE OCTAL VALUE OF GIVEN HEXADECIMAL VALUE IS : \");
    for(i=l-1;i>=0;i--)
    {
        printf(\"%d\",rev[i]);
    }
    printf(\".\");
    for(k=0;k<j;k++)
    {
        printf(\"%d\",a[k]);
    }
    getch();
}
/* OUTPUT
--------------------------------------------------------------------
ENTER THE HEXADECIMAL VALUE : 45.43

THE OCTAL VALUE OF GIVEN HEXADECIMAL VALUE IS : 1000101.0100
*/

 
 
Didn't find what you were looking for? Find more on PRORAM TO CONVERT HEXADECIMAL VALUE TO OCTAL USING FLOAT VALUES