# Java Program to find Number Combination

You are Here:

## Find Number Combination

In the following example, we will find all possible combinations of numbers with 4 and 8 within the limit 500.

### Example

Java Compiler
import java.util.Scanner; public class myClass { public static void main(String[] args) { int combination = 2; int[] num = {4, 8}; int limit = 500; int i, j, lastDigit, copyNum, digit, count; System.out.print("List of combinations of 4 and 8 upto 500:\n"); // Iterate from 1 to limit for(i=1; i<=limit; i++) { copyNum = i; count = 0; digit = 0; // Check each digit starting from last digit while(copyNum != 0) { count++; lastDigit = copyNum % 10; for(j=0; j<combination; j++) { if(num[j] == lastDigit) digit++; } copyNum = copyNum / 10; } // result if(count == digit) System.out.print(i +" "); } } }

### Output

List of combinations of 4 and 8 upto 500: 4 8 44 48 84 88 444 448 484 488

## Find Number Combination for any Given Numbers

In the following example, we will find all possible combinations of the given numbers within the given limit.

### Example

Java Compiler
import java.util.Scanner; public class myClass { public static void main(String[] args) { Scanner reader = new Scanner(System.in); int i, j, lastDigit, copyNum, digit, count; int[] num = new int[50]; System.out.print("Enter the number of Combination: "); int combination = reader.nextInt(); for(i=0; i<combination; i++) { System.out.format("Enter Digit %d: ", i+1); num[i] = reader.nextInt(); } System.out.format("Enter the Limit: "); int limit = reader.nextInt(); // Iterate from 1 to limit for(i=1; i<=limit; i++) { copyNum = i; count = 0; digit = 0; // Check each digit starting from last digit while(copyNum != 0) { count++; lastDigit = copyNum % 10; for(j=0; j<combination; j++) { if(num[j] == lastDigit) digit++; } copyNum = copyNum / 10; } // result if(count == digit) System.out.print(i +" "); } } }

### Output

Enter the number of Combination: 2 Enter Digit 1: 2 Enter Digit 2: 1 Enter the Limit: 500 1 2 11 12 21 22 111 112 121 122 211 212 221 222

## Reminder

Hi Developers, we almost covered 90% of String functions and Interview Question on Java with examples for quick and easy learning.

We are working to cover every Single Concept in Java.