# Thread: Hi there I'm new beginner. Decimal/Octal converter help/

1. ## Hi there I'm new beginner. Decimal/Octal converter help/

I downloaded this code from some site after a search. As you can see it has a limit(295600127). Why? is there a mistake in the code. . anyways I want to make it work correctly. With someones help of course. Basically explail what is going on below the first return 0;

Code:
```/*295600127currentlimit*/

#include <stdio.h>
#include <math.h>

int convertDecimalToOctal(int decimalNumber);
int main()
{
int decimalNumber;

printf("Enter a decimal number: ");
scanf("%d", &decimalNumber);

printf("%d in decimal = %d in octal", decimalNumber, convertDecimalToOctal(decimalNumber));

return 0;
}

int convertDecimalToOctal(int decimalNumber)
{
int octalNumber = 0, i = 1;

while (decimalNumber != 0)
{

octalNumber += (decimalNumber % 8) * i;
decimalNumber /= 8;
i *= 10;
}

return octalNumber;
}```
Just to prove i'm a beginner this is my own dec/oct converter:
It would only do up 2147483647 until I figured out to unsign my int's
I guess I can't wordwrap my code huh?

Code:
```/*decimal_octal_converter*/

#include <stdio.h>
/*---------------*/

unsigned int dec, oct;

int main(void)

{
printf("Enter a number between 0 and 4294967295: ");
scanf ("%d", &dec);

printf("Octal value: %d%d%d%d%d%d%d%d%d%d%d", dec/(8*8*8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8), dec%(8*8*8*8*8*8*8)/(8*8*8*8*8*8), dec%(8*8*8*8*8*8)/(8*8*8*8*8), dec%(8*8*8*8*8)/(8*8*8*8), dec%(8*8*8*8)/(8*8*8), dec%(8*8*8)/(8*8), dec%(8*8*8)%(8*8)/8, dec%8);

return 0;
}```

And this is the easiest one:
It took me a while to learn that %o means substitute the decimal number with it's equivalent octal value. In fact if yoy replace the o with an x it wil give the hex value. I couldn't figure out how just modulus%ozero could do all that figuring. Obviously I now know that's not a zero but an "o".

Code:
```/* C Program to Convert Decimal to Octal Number */

#include <stdio.h>
int main()
{
int number;
printf("\n Please Enter the Number You want to Convert : ");
scanf("%d", &number);

printf("\n Octal Number of a Given Number = %o", number);

return 0;
}``` 2. > As you can see it has a limit(295600127). Why? is there a mistake in the code.
Because it's faking it by storing only digits 0 through 7 in an otherwise decimal integer.

295600127 decimal is 2147477777 octal.
But the first code just stores it in a decimal integer (that's the %d format), and that's close to the max decimal value of 2147483647

> I guess I can't wordwrap my code huh?
You can add newlines almost anywhere you can use space.
Code:
```printf("Octal value: %d%d%d%d%d%d%d%d%d%d%d",
dec/(8*8*8*8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8*8)/(8*8*8*8*8*8*8),
dec%(8*8*8*8*8*8*8)/(8*8*8*8*8*8),
dec%(8*8*8*8*8*8)/(8*8*8*8*8),
dec%(8*8*8*8*8)/(8*8*8*8),
dec%(8*8*8*8)/(8*8*8),
dec%(8*8*8)/(8*8),
dec%(8*8*8)%(8*8)/8,
dec%8);``` 3. Originally Posted by Salem Because it's faking it by storing only digits 0 through 7 in an otherwise decimal integer.
Binary Coded Decimal (BCD) is a thing, where we "fake" storing decimal digits in an otherwise binary integer. Is this thread just an example of Decimal Coded Octal?  4. Originally Posted by christop Binary Coded Decimal (BCD) is a thing, where we "fake" storing decimal digits in an otherwise binary integer. Is this thread just an example of Decimal Coded Octal? I'm a beginner I can't fake anything. If youcan would you tell me exactly what every line of this snippet does. I know it is part of that long function highlighted. I also know it creates 2 variables
octalnumber=0 and i=1 I also know what modulos does.
Code:
```int convertDecimalToOctal(int decimalNumber){
int octalNumber = 0, i = 1;

while (decimalNumber != 0)this says while decimalNumber is not zero
do the following
{

octalNumber += (decimalNumber % 8) * i;(what this does)
decimalNumber /= 8;(what this does)
i *= 10;(what this does)
}

return octalNumber;
}``` Popular pages Recent additions dec%/, decimalnumber, int, number, return 