// OCTAL to DECIMAL
#include <stdio.h>
#include <math.h>
#include <string.h>
int main()
{
int n=0,POWER=0,i,j;
char ar1[20];
char ar2[8] = {'0','1','2','3','4','5','6','7'};
/*
ar2[0]=0,ar2[1]=1,ar2[2]=2,ar2[3]=3,
ar2[4]=4,ar2[5]=5,ar2[6]=6,ar2[7]=7
*/
printf("Enter any Octal Number : ");
scanf("%s",&ar1);
//let ar1 = 372
for(i=strlen(ar1)-1;i>=0;i--)
//i=2,1,0
{
for(j=0;j<8;j++)
{
if(ar1[i]==ar2[j])
{
n=n+j*pow(8,POWER);
}
}
POWER++;
/*
step 1:
ar1[2]=2=ar2[2],it means j=2
n = 0+2*pow(8,0) = 0+2 = 2
POWER=1
step 2:
ar1[1]=7=ar2[7],it means j=7
n = 2+7*pow(8,1) = 2+56 = 58
POWER=2
step 2:
ar1[2]=3=ar2[3],it means j=3
n = 58+3*pow(8,2) = 58+192 = 250
POWER=3
n = 250
*/
}
printf("\nDecimal Number = %d",n);
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.