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; } } }
array in reverse order
Can someone help me print the results of this array in reverse order t
Page 1 of 14 Replies  2971 Views  Last Post: 17 February 2009  06:26 AM
#1
array in reverse order
Posted 13 February 2009  06:57 AM
Replies To: array in reverse order
#2
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.length1 to 0
hope this helps
for(int i = array.length1; i >= 0; i) System.out.print(array[i] + " ");
hope this helps
#3
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.length1; a >= 0; a){
System.out.print(array[a] + "Therefore, the binary value of " + n + " is ");
}
}
}
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.length1; a >= 0; a){
System.out.print(array[a] + "Therefore, the binary value of " + n + " is ");
}
}
}
#4
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
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
Page 1 of 1
