dimanche 29 mars 2015

How do I fix this program that transforms into bit pattern all numbers that are multiply of 5 between 5 and 100 (inclusive)



How do I make this program count from 5 to 100 using for loops. For example it should be like this:

bit pattern representation of 5 is 101.

bit pattern representation of 10 is 1010.

bit pattern representation of 15 is 1111


I already did the code to convert decimal to binary, but I want it to start at 5 and go to 100 from multiples of 5. I know you have to put a for loop statement somewhere but don't know where.



public static void main(String[] args) {

int x=5;
String binaryString = " ";
int remainder = 0;
int decimalNumber=x;

for (int i = 1; decimalNumber > 0; i++) {
binaryString = String.valueOf(decimalNumber&1) + binaryString;
remainder = decimalNumber % 2;
decimalNumber /= 2;
}

System.out.println("bit pattern representation of "+x+" is "+binaryString);
}



Aucun commentaire:

Enregistrer un commentaire