# array in reverse order

Page 1 of 1

## 4 Replies - 3041 Views - Last Post: 17 February 2009 - 06:26 AMRate 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=86539&amp;s=555d73923e68b6db73cacd341271b243&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 TrainerGodd

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 10-February 09

# array in reverse order

Posted 13 February 2009 - 06:57 AM

```
import java.util.*;
public class DecimalToBinaryConverter {

public static void main(String[] args) {
int n;
int w;
int i;
int z;
int remainder;

Scanner sc = new Scanner(System.in);
System.out.println("Please enter the starting number: ");
n = sc.nextInt();
z = n;
w = n;

int count = 0;
int[] array = new int[count];

while (w > 0) {
remainder = w % 2;
i = w;
w = w / 2;
System.out.println("Decimal " + i + " divided by 2 equals " + w + " with a remainder of:  " + remainder);
count++;
}

System.out.print("Therefore, the binary value of " + n + " is ");
while (count > 0) {
System.out.print(array.length);
count--;
}

}
}

```

Is This A Good Question/Topic? 0

## Replies To: array in reverse order

### #2 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: array in reverse order

Posted 13 February 2009 - 07:20 AM

you need to iterate through the array in reverse order, that is start from the last element which is at index array.length-1 to 0
```for(int i = array.length-1; i >= 0; i--)
System.out.print(array[i] + " ");

```

hope this helps

### #3 TrainerGodd

• New D.I.C Head

Reputation: 0
• Posts: 5
• Joined: 10-February 09

## Re: array in reverse order

Posted 17 February 2009 - 06:11 AM

I am still having some difficulties. I added the code you suggested, but I did not get any additional output from what I was seeing before. It just gives me what the conversion is doing, but it does not show me the final line of output I need that should say "Therefore, the binary value of n is (and then the binary value)." Here is my output and then my code after that. Please help. Thanks!
run:
Please enter the starting number:
250
Decimal 250 divided by 2 equals 125 with a remainder of: 0
Decimal 125 divided by 2 equals 62 with a remainder of: 1
Decimal 62 divided by 2 equals 31 with a remainder of: 0
Decimal 31 divided by 2 equals 15 with a remainder of: 1
Decimal 15 divided by 2 equals 7 with a remainder of: 1
Decimal 7 divided by 2 equals 3 with a remainder of: 1
Decimal 3 divided by 2 equals 1 with a remainder of: 1
Decimal 1 divided by 2 equals 0 with a remainder of: 1
BUILD SUCCESSFUL (total time: 4 seconds)

import java.util.*;
public class DecimalToBinaryConverter {

public static void main(String[] args) {
int n;
int w;
int i;
int z;
int remainder;

Scanner sc = new Scanner(System.in);
System.out.println("Please enter the starting number: ");
n = sc.nextInt();
z = n;
w = n;

int count = 0;
int[] array = new int[count];

while (w > 0) {
remainder = w % 2;
i = w;
w = w / 2;
System.out.println("Decimal " + i + " divided by 2 equals " + w + " with a remainder of: " + remainder);
count++;
}

for(int a = array.length-1; a >= 0; a--){
System.out.print(array[a] + "Therefore, the binary value of " + n + " is ");
}

}
}

### #4 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: array in reverse order

Posted 17 February 2009 - 06:14 AM

so what exactly do you want to do? coz i didnt understand

### #5 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: array in reverse order

Posted 17 February 2009 - 06:26 AM

ok, i think this is what you want
```import java.util.Scanner;
public class Player
{
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.println("enter a decimal number to convert");
int n = sc.nextInt();
String s = "";
while(n != 0)
{
System.out.println("Decimal value " + n + " divided by 2 equals " + (n/2) + " with a remainder of " + (n%2));
s = (n%2) + s;
n = n/2;
}
System.out.println("thus the binary value is " +s);
}
}

```

This post has been edited by mostyfriedman: 17 February 2009 - 06:29 AM