954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Octal to Decimal number conversion program

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;

}
newport
Newbie Poster
8 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>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;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

newport
Newbie Poster
8 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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 .

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You