# COME ON CODE ON

A blog about programming and more programming.

## Number System Conversion

I gave this question in one of the contests to interconvert between decimal,octal and hexdecimal numbers without traversing the digits. The numbers could be random like 12 0xA 15 o12. You have to print the number in other two systems except from the one entered. The program would be terminated by a negative number. The solution i gave uses sscanf and was as following :

```int main()
{
char str[20];
int dec,l,i,n;
while (1)
{
scanf("%s",str);
if (str[0]=='-')
break;
else if (str[1]=='x')
{
str[0]=str[1]=' ';
sscanf(str,"%X",&amp;n);
printf("%d o%o\n",n,n);
}
else if (str[0]=='o')
{
str[0]=' ';
sscanf(str,"%o",&amp;n);
printf("%d 0x%X\n",n,n);
}
else
{
n=atoi(str);
printf("0x%X o%o\n",n,n);
}
}
}```

If you have a better solution please post it in the comments.

-fR0D

Written by fR0DDY

February 23, 2009 at 12:55 PM

Posted in Maths, Programming

Tagged with ,

### 3 Responses

1. hehe.. nice trick. 😀

jrharshath

February 23, 2009 at 2:32 PM

2. sscanf , sprintf
ye mujhe book me nahi mile .

manoj

February 24, 2009 at 12:00 PM