Decimal to octal conversion

1

I need to create an algorithm that converts decimal to octal, but my conversion is wrong, can anyone help me how would it be right?

  #include <stdio.h>  

int decim(int n)    
{    
    int i,a;    
    int octal;    


    if(n<=7)  
    {  
        octal=n;  
    }  

    else    
    {  

        while(n>=8 )   
            {       
                a=n%8;  
                n=n/8;  
            }  
            n=n%8;

    }

return octal;

}


int main()  
{
    int n,octal;  

    printf("Informe um numero decimal:");  
    scanf("%d",&n);  

    octal=decim(n);  

    printf(" o numero octal eh: %d\n",octal);    



    return 0;    
}
    
asked by anonymous 27.05.2018 / 01:06

3 answers

4

The code you have does not use the octal variable that is the one you use to return the calculated value, so it will always give you the random value to get in memory, unless you enter the first case of n<=7 .

Also, you are not using the a that corresponds to each of the digits that will form the octal value, with the particularity that they are obtained from right to left.

Trying to make the most of the code you already have, and letting it work you need:

  • initialize octal and increase its value to each digit it interprets
  • Initialize the i and use it as the base factor 10, to create the digits in reverse.

Implementing these points I mentioned would look like this:

int decim(int n) {
    int i = 1,a; //i inicializado a 1
    int octal = 0; //octal inicializado a 0

    if(n<=7) {
        octal=n;
    }
    else {
        while(n>=8) {
            a=n%8;
            n=n/8;
            octal = octal + a * i; //calculo do octal
            i *= 10; //aumento do fator de base 10
        }
        n=n%8;
        octal = octal + n * i; //mesmo calculo aqui
    }

    return octal;
}

See the code working on Ideone

Of course the code can be more compact and optimized, but I've tried my best to use what I already had instead of rewriting logic.

    
27.05.2018 / 03:16
2

The remainder of the division has to be stored in some array (the octal variable value is not changed inside its array) and then inverted to show the result:

#include <stdio.h>

void decimal_to_octal(int num) {
    int result[15] = {};
    int i = 0, j = 0;

    if(num >= 8) {
        printf("Decimal: %d -> Octal: ", num);

        while(num >= 8) {
            result[i++] = num % 8;
            num = num / 8;  
        }
        result[i] = num;

        // Imprimindo o valor invertido sem os zeros à esquerda
        for(j = i; j >= 0; j--) {
            printf("%d", result[j]);
        }
        printf("\n");
    } else {
        printf("Decimal: %d -> Octal: %d\n", num, num);
    }
}

int main(void) {

    decimal_to_octal(7); // 7
    decimal_to_octal(10); // 12
    decimal_to_octal(568); // 1070
    decimal_to_octal(3578); // 6772
    return 0;
}

You can check the solution at ideone .

    
27.05.2018 / 03:15
0

The problem is the inside of the "while".

At each step, you must add to the previous result (variable octal ), the figure found by the rest of the current division, shifted in its proper position (exponent: 10⁰, 10¹, 10², etc. .).

The algorithm would be:

while n!=0 {
            octal = octal + i . n%8
            i = i . 10
            n = n/8
            }

Initializing octal = 0 and i = 1

In this case, there is no need for "if (n

27.05.2018 / 05:00