bomba923
- 759
- 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 {
. [/color]public static void main(String args[]) {
. [/color]int base = 0, dgts = 0;
. [/color]int[] bsq;
. [/color]BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
. [/color]try {
. [/color]System.out.println("Enter a natural base:");
. [/color]base = Integer.parseInt(br.readLine ());
. [/color]System.out.println("Enter the maximum number of digits:");
. [/color]dgts = Integer.parseInt(br.readLine ());
. [/color]}//try
. [/color]catch(IOException e) {}
. [/color]bsq = new int[dgts];
. [/color]for(int i=0; i<Math.pow(base,dgts); i++) {
. [/color]for(int k=0; k<dgts; k++) {
. [/color]bsq[k] = i / (int) Math.pow(base,k) % base;
. [/color]System.out.print(bsq[dgts-k-1]);
. [/color]}//for
. [/color]System.out.println();
. [/color]}//for
. [/color]}//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 {
. [/color]public static void main(String args[]) {
. [/color]int base = 0, dgts = 0;
. [/color]int[] bsq;
. [/color]BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
. [/color]try {
. [/color]System.out.println("Enter a natural base:");
. [/color]base = Integer.parseInt(br.readLine ());
. [/color]System.out.println("Enter the maximum number of digits:");
. [/color]dgts = Integer.parseInt(br.readLine ());
. [/color]}//try
. [/color]catch(IOException e) {}
. [/color]bsq = new int[dgts];
. [/color]for(int i=0; i<Math.pow(base,dgts); i++) {
. [/color]for(int k=0; k<dgts; k++) {
. [/color]bsq[k] = i / (int) Math.pow(base,k) % base;
. [/color]System.out.print(bsq[dgts-k-1]);
. [/color]}//for
. [/color]System.out.println();
. [/color]}//for
. [/color]}//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

Even more, the program outputs 0100 when it should output 1000.
Why is this so??
Last edited: