KnowledgeBoat Logo

Computer Science

Write a program in Java to enter a natural number, where N>100 and N<1000, the natural number must not contain zeros. Print all the combinations of digits of the number including the number itself. Each new combination should appear on a new line. The program displays a message "Invalid Number", if N <100 or contains zeros.

Sample Input: Enter a number: 465
Sample Output:
456
465
546
564
645
654

Sample Input: Enter a number: -712
Sample Output: Invalid Number

Sample Input: Enter a number: 960
Sample Output: Invalid Number

Java

Java Iterative Stmts

ICSE

2 Likes

Answer

import java.util.Scanner;

public class KboatNumberCombinations
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int n = in.nextInt();
        if (n < 100 || n > 999) {
            System.out.println("Invalid Number");
            return;
        }
        
        int digits[] = new int[3];
        int t = n;
        int idx = 2;
        while (t != 0) {
            int d = t % 10;
            if (d == 0) {
                System.out.println("Invalid Number");
                return;
            }
            digits[idx--] = d;
             t /= 10;
        }
        
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                for (int k = 0; k < 3; k++) {
                     if (i != j && j != k && i != k)
                        System.out.println(digits[i] 
                                            + "" + digits[j] 
                                            + "" + digits[k]);
                }
            }
        }
    }
}

Output

BlueJ output of Write a program in Java to enter a natural number, where N>100 and N 1000, the natural number must not contain zeros. Print all the combinations of digits of the number including the number itself. Each new combination should appear on a new line. The program displays a message "Invalid Number", if N 100 or contains zeros. Sample Input: Enter a number: 465 Sample Output: 456 465 546 564 645 654 Sample Input: Enter a number: -712 Sample Output: Invalid Number Sample Input: Enter a number: 960 Sample Output: Invalid NumberBlueJ output of Write a program in Java to enter a natural number, where N>100 and N 1000, the natural number must not contain zeros. Print all the combinations of digits of the number including the number itself. Each new combination should appear on a new line. The program displays a message "Invalid Number", if N 100 or contains zeros. Sample Input: Enter a number: 465 Sample Output: 456 465 546 564 645 654 Sample Input: Enter a number: -712 Sample Output: Invalid Number Sample Input: Enter a number: 960 Sample Output: Invalid NumberBlueJ output of Write a program in Java to enter a natural number, where N>100 and N 1000, the natural number must not contain zeros. Print all the combinations of digits of the number including the number itself. Each new combination should appear on a new line. The program displays a message "Invalid Number", if N 100 or contains zeros. Sample Input: Enter a number: 465 Sample Output: 456 465 546 564 645 654 Sample Input: Enter a number: -712 Sample Output: Invalid Number Sample Input: Enter a number: 960 Sample Output: Invalid Number

Answered By

1 Like


Related Questions