- #1
bomba923
- 763
- 0
Very simple...but why doesn't it work!?
The program should output every number of a user-input base, containing a certain quantity of digits.
------------------------------------------
import java.io.*;
public class BaseCount {
. public static void main(String args[]) {
. int base = 0, dgts = 0;
. int[] bsq;
. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
. try {
. System.out.println("Enter a natural base:");
. base = Integer.parseInt(br.readLine ());
. System.out.println("Enter the maximum number of digits:");
. dgts = Integer.parseInt(br.readLine ());
. }//try
. catch(IOException e) {}
. bsq = new int[dgts];
. for(int i=0; i<Math.pow(base,dgts); i++) {
. for(int k=0; k<dgts; k++) {
. bsq[k] = i / (int) Math.pow(base,k) % base;
. System.out.print(bsq[dgts-k-1]);
. }//for
. System.out.println();
. }//for
. }//main
}//BaseCount
---------------------------------------
However, there seem to be problems!
For example, let base = 2 and dtgs = 4.
The program outputs:
The program does not output 0100 when i = 4.
Even more, the program outputs 0100 when it should output 1000.
Why is this so??
The program should output every number of a user-input base, containing a certain quantity of digits.
------------------------------------------
import java.io.*;
public class BaseCount {
. public static void main(String args[]) {
. int base = 0, dgts = 0;
. int[] bsq;
. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
. try {
. System.out.println("Enter a natural base:");
. base = Integer.parseInt(br.readLine ());
. System.out.println("Enter the maximum number of digits:");
. dgts = Integer.parseInt(br.readLine ());
. }//try
. catch(IOException e) {}
. bsq = new int[dgts];
. for(int i=0; i<Math.pow(base,dgts); i++) {
. for(int k=0; k<dgts; k++) {
. bsq[k] = i / (int) Math.pow(base,k) % base;
. System.out.print(bsq[dgts-k-1]);
. }//for
. System.out.println();
. }//for
. }//main
}//BaseCount
---------------------------------------
However, there seem to be problems!
For example, let base = 2 and dtgs = 4.
The program outputs:
Code:
Enter a natural base:
2
Enter the maximum number of digits:
4
0000
0001
0010
0011
0000
0101
0110
0111
0100
1001
1010
1011
1000
1101
1110
1111
The program does not output 0100 when i = 4.
Even more, the program outputs 0100 when it should output 1000.
Why is this so??
Last edited: