ARPIT SINGH
ARPIT SINGH

Reputation: 1

C Programming : Decimal to Octal

Consider:

// Decimal into octalenter image description here
#include <stdio.h>
#include <math.h>

int main()
{
    int num, r = 0, octal, i = 1;

    printf("Enter the number you want to convert into ocal form: ");
    scanf("%d", &num);

    while (num)
    {                                   // num = 123    // num = 15     // num = 1
        r = num % 8;                    // r = 3        // r = 7        // r = 1
        octal = octal + r * pow(10, i); // octal = 3    // octal = 73   // octal = 173
        i++;                            // i == 1       // i = 2        // i = 3
        num = num / 8;                  // num = 15     // num = 1      // num = 0
    }
    printf("Octal equivalent is %d",  octal);
    return 0;
}

// Output
Enter the number you want to convert into ocal form: 16
Octal equivalent is 2396360

The output is not what it has to be. I think I applied the logic correctly, but... What is my mistake?

Upvotes: -1

Views: 532

Answers (3)

John Bollinger
John Bollinger

Reputation: 180351

In the first place, your approach to the problem is only in the grossest sense characterizable as converting decimal to octal. A better description would be that it is (trying to)

  1. convert decimal to the implementation's internal integer representation, which is likely characterizable as base-2, and / or base-28 and / or base-232.

  2. compute a new number, also in the machine's internal integer representation, such that

  3. when the new number is converted to decimal format on output, the decimal digit sequence corresponds to the octal digit sequence of the original number.

Perhaps that's exactly what you are expected to do, but it's absurd, really. If that implementation approach is required by an exercise then the source of that exercise should not be trusted for C-language instruction. The whole concept obscures the difference between internal and external representation.

If you are allowed to use any approach you like to read numbers in decimal format and output corresponding octal representations, and you are not required to handle negative numbers, then a more reasonable approach is to use a scanf-family function's %u conversion specifier to interpret the input (as an unsigned decimal integer), and then simply use printf's %o conversion specifier to output that integer in octal format. (If you need to handle negative numbers then you can do similar, but a little extra work will be needed to deal with the signedness.) No explicit conversion computation is required. There's so little to that that I cannot easily present an example without working the whole exercise for you.

As for the actual code presented in the question,

  • you need to initialize octal to 0
  • do not use pow() for small integer exponents. Use ordinary multiplication instead. In this case, I would maintain a variable representing the place value of the current output digit, starting at 1, and multiply it by 10 at the end of each loop iteration. Mutliply each digit by that instead of by the result of a pow() call.
  • if you insist on using pow(), then the exponent for the least-significant digit should be 0, not 1, and you should take care to round() the floating-point result to an integer before using it.

Upvotes: 2

gulpr
gulpr

Reputation: 4598

  1. octal is not initalized
  2. do not use floating point numbers and functions (pow) when you deal with integers
  3. you do not convert to octal and you will very quickly overflow the integer.

If you want to print as octal (and it is exactly what you do):

void printOctal(int x)
{
    if(!(x / 8)) printf("%d", x);
    else 
    { 
        printOctal(x / 8);
        printf("%d", abs(x %8));
    }
}

int main(void)
{
    printOctal(16);
}

https://godbolt.org/z/7hEc5dqrK

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222923

You do not initialize octal, and you initialize i to 1. Both of them should be initialized to 0.

It is also a bad idea to use pow to calculate integer powers of 10. (Because it is wasteful and because some implementations of pow are bad and do not return accurate results.) Just start a running product at 1 and multiply it by 10 in each iteration.

Upvotes: 3

Related Questions