# Help with Decimal to Octal conversion

Page 1 of 1

## 6 Replies - 3533 Views - Last Post: 19 September 2009 - 06:22 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=126822&amp;s=c84bda7c35dc97ab793ce821140e0086&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Angraa

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 19-September 09

# Help with Decimal to Octal conversion

Posted 19 September 2009 - 03:54 PM

```import java.util.Scanner;
public class cloveless_Octal
{
public static void main ( String args[] )
{
int x, o1, o2, o3, o4, o5;
Scanner in = new Scanner ( System.in );
System.out.print ( "Please enter a number between 0 and 32767 to convert:" );
x = in.nextInt();
o1 = x/8^5;
o2 = x/8^4;
o3 = x/8^3;
o4 = x/8^2;
o5 = x/8^1;
System.out.printf ( "Your integer " + x );
System.out.printf ( " is " +o5, o4, o3, o2, o1 );
System.out.printf ( " in octal." );
}}

```

I am not sure what is going wrong. As of right now, I can compile the program, but if I were to enter "15" as the input , the program tells me that the octal equivalent is "0".

I am very new to Java and this is for an assignment. We must use the "/" and "%" operators and I know I am missing something, but I am not sure where to go from here. All I really need is a point in the right direction and I should be able to go from there.

Thank you!

P.S. Could someone explain what modulus "%" does?

Is This A Good Question/Topic? 0

## Replies To: Help with Decimal to Octal conversion

### #2 Locke

• Sarcasm Extraordinaire!

Reputation: 526
• Posts: 5,604
• Joined: 20-March 08

## Re: Help with Decimal to Octal conversion

Posted 19 September 2009 - 03:59 PM

Well, I'll start with the modulus. Modulus returns the remainder when you do division.

7 / 3 = 2.
7 % 3 = 1, since the remainder is 1 when you do 7 / 3.

But you're trying to use the ^ operator, and it's not doing what you think it's doing. It has nothing to do with exponents. That operator is a bit-wise exclusive-or. You'll want Math.pow(8, EXPONENT);

Hope this helps!
Was This Post Helpful? 0

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Help with Decimal to Octal conversion

Posted 19 September 2009 - 04:00 PM

You are not using the most efficient way
Better to use the modulo operator for that
o1 = num % 8;
num /= 8;
o2 = num % 8;
num /= 8;
....
Was This Post Helpful? 0

### #4 Angraa

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 19-September 09

## Re: Help with Decimal to Octal conversion

Posted 19 September 2009 - 04:15 PM

Locke, thank you for clarifying modulus and telling me about the ^, but I am not sure how to use the other information you gave me.

pbl, I typed in the code as you gave the example, but the numbers are still way off. It is, for some reason, not even reading the input correctly now.

```import java.util.Scanner;
public class cloveless_Octal
{
public static void main ( String args[] )
{
int x, o1, o2, o3, o4, o5;
Scanner in = new Scanner ( System.in );
System.out.print ( "Please enter a number between 0 and 32767 to convert:" );
x = in.nextInt();
o1 = x % 8;
x /=8;
o2 = x % 8;
x /=8;
o3 = x % 8;
x /=8;
o4 = x % 8;
x /=8;
o5 = x % 8;
x /=8;
System.out.printf ( "Your integer " + x );
System.out.printf ( " is " +o5, o4, o3, o2, o1 );
System.out.printf ( " in octal." );
}}

```

I probably just didn't put it in correctly.
Was This Post Helpful? 0

### #5 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Help with Decimal to Octal conversion

Posted 19 September 2009 - 04:22 PM

printf requires a formatter in the first String and then the arguments to match these formatters
System.out.printf ( " is " +o5, o4, o3, o2, o1 );

should be

System.out.printf ( " is %d%d%d%d%d ", o5, o4, o3, o2, o1 );
Was This Post Helpful? 0

### #6 Angraa

• New D.I.C Head

Reputation: 0
• Posts: 3
• Joined: 19-September 09

## Re: Help with Decimal to Octal conversion

Posted 19 September 2009 - 04:48 PM

Okay the program can convert now, but for some reason I am getting a problem on the print. I put in "15" for the input and the message comes up saying:
Your integer 0 is 00017 in octal.

So why is it giving me 0? It wasn't doing that on my first iteration of the program.

Thanks again. ^.^

Edit:
Here is the code
```import java.util.Scanner;
public class cloveless_Octal
{
public static void main ( String args[] )
{
int x, o1, o2, o3, o4, o5;
Scanner in = new Scanner ( System.in );
System.out.print ( "Please enter a number between 0 and 32767 to convert: " );
x = in.nextInt();
o1 = x % 8;
x /=8;
o2 = x % 8;
x /=8;
o3 = x % 8;
x /=8;
o4 = x % 8;
x /=8;
o5 = x % 8;
x /=8;
System.out.printf ( "Your integer " +x );
System.out.printf ( " is %d%d%d%d%d ", o5, o4, o3, o2, o1 );
System.out.printf ( " in octal." );

```

This post has been edited by Angraa: 19 September 2009 - 04:49 PM

Was This Post Helpful? 0

### #7 pbl

• There is nothing you can't do with a JTable

Reputation: 8365
• Posts: 31,956
• Joined: 06-March 08

## Re: Help with Decimal to Octal conversion

Posted 19 September 2009 - 06:22 PM

yes you'll have to save the value of x before starting to / % it

x = in.nextInt();
int originalX = x;
...
/ and %
...
System.out.printf ( "Your integer " + originalX );
System.out.printf ( " is %d%d%d%d%d ", o5, o4, o3, o2, o1 );
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }