I'm trying to make a program that converts octal numbers to decimal numbers. I know the concept is taking the %10 of the number to get the first digit, /10 the number then take the %10 to get the second digit and multiplying by increase powers of 8, but I have no idea how to translate this to a program. Could someone please guide me in the right direction.

#include <stdio.h>

int main ()

{

        int num1, base8, x, y, n, lastDigit;

        printf("Enter an integer in base 8: ");
        scanf("%d", &num1);

        num1 %10 =lastDigit;
        
        n = 1;
        x=num1
        
        if(num1 > 10){
        x/10 = x;
        x %10 =y;
        
        while (x != y) {
        
        x/10 = x;
        x %10 =y;
        base8 = y*8^n
        n = n + 1;   
        }}
        sum= base8+lastDigit;
        printf("%d", sum);
        
return 0;

}

Recommended Answers

All 5 Replies

It would help if you'd format your code so we can follow it's flow. Proper indentation is crucial to understanding.

>I'm trying to make a program that converts octal numbers to decimal numbers.
I don't suppose this would work for you:

#include <stdio.h>

int main ( void )
{
  int number;

  scanf ( "%o", &number );
  printf ( "%d\n", number );

  return 0;
}

it does works, but i believe i am suppose to come up with a formula. thanks.

Firstly, base8 = y*8^n does not have semi-colon at the end of the statement. Secondly, ^ is not a power operator. I think you are confusing with Visual Basic. Moreover, x/10 = x; and x %10 =y; are not right. It should be x = x / 10 .

>i believe i am suppose to come up with a formula
I figured, but it was worth a shot. My advice is to figure out the exact steps you use to do this conversion on paper. It's hard to tell the computer how to do something if you can't lay out the steps one by one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.