w3resource

Java: Display a three-digit number using 1, 2, 3, 4

Java Basic: Exercise-39 with Solution

Write a Java program to create and display a unique three-digit number using 1, 2, 3, 4. Also count how many three-digit numbers are there.

Pictorial Presentation:

Java: Create and display unique three-digit number using 1, 2, 3, 4.

Sample Solution:

Java Code:

import java.util.Scanner;

public class Exercise39 {
    public static void main(String[] args) {
        int amount = 0; // Initialize a counter for three-digit numbers
        
        // Iterate through three nested loops to generate all unique three-digit numbers
        for (int i = 1; i <= 4; i++) {
            for (int j = 1; j <= 4; j++) {
                for (int k = 1; k <= 4; k++) {
                    // Check if 'i,' 'j,' and 'k' are all different (not equal)
                    if (k != i && k != j && i != j) {
                        amount++; // Increment the counter
                        System.out.println(i + "" + j + "" + k); // Print the generated three-digit number
                    }
                }
            }
        }
        
        // Display the total number of generated three-digit numbers
        System.out.println("Total number of the three-digit-number is " + amount);
    }
}

Explanation:

In the exercise above -

  • First, the variable 'amount' is initialized to 0. This variable will be used to count the total number of three-digit numbers that meet the criteria.
  • The code uses three nested for loops to generate all possible combinations of three digits from 1 to 4.
  • The outermost loop iterates through the first digit (i) from 1 to 4.
  • The middle loop iterates through the second digit (j) from 1 to 4.
  • The innermost loop iterates through the third digit (k) from 1 to 4.
  • Within the innermost loop, there is a conditional check if(k != i && k != j && i != j). This condition ensures that 'i', 'j', and 'k' are all distinct digits, meaning they are not equal to each other. This avoids repetition in the generated numbers.
  • When a valid combination is found (i.e., three distinct digits), it increments the amount variable, prints the three-digit number in the format 'ijk', and moves on to the next combination.
  • Finally, all possible combinations are generated and counted, it prints the total number of three-digit numbers that meet the criteria.

Sample Output:

123                                                                                                      
124                                                                                                      
132                                                                                                      
134                                                                                                      
142                                                                                                      
143                                                                                                      
213                                                                                                      
214                                                                                                      
231                                                                                                      
234                                                                                                      
241  
------
431                                                                                                      
432                                                                                                      
Total number of the three-digit-number is 24 

Flowchart:

Flowchart: Java exercises: Create and display unique three-digit number

Java Code Editor:

Previous: Write a Java program to count the letters, spaces, numbers and other characters of an input string.
Next: Write a Java program to list the available character sets in charset objects.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.