# Java Exercises: Reads n digits (given) chosen from 0 to 9 and prints the number of combinations

## Java Basic: Exercise-228 with Solution

Write a Java program that reads n digits (given) chosen from 0 to 9 and prints the number of combinations where the sum of the digits equals to another given number (s). Do not use the same digits in a combination.

For example, the combinations where n = 3 and s = 6 are as follows:

1 + 2 + 3 = 6

0 + 1 + 5 = 6

0 + 2 + 4 = 6

**Input:**

Two integers as number of combinations and their sum by a single space in a line. Input 0 0 to exit.

**Pictorial Presentation:**

**Sample Solution:**

**Java Code:**

```
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
System.out.println("Input number of combinations and sum (separated by a space in a line):");
int n = stdIn.nextInt();
int s = stdIn.nextInt();
int c1 = comnum(0, n, s,0);
System.out.println("Number of combinations:");
System.out.println(c1);
}
public static int comnum(int i, int n, int s,int p) {
if(s == p && n == 0) {
return 1;
}
if(i >= 10) {
return 0;
}
if(n < 0) {
return 0;
}
if(p > s) {
return 0;
}
int c1 = comnum(i+1,n-1,s,p+i);
int c2 = comnum(i+1,n,s,p);
return c1+c2;
}
}
```

Sample Output:

Input number of combinations and sum (separated by a space in a line): 3 6 Number of combinations: 3

**Flowchart: **

**Java Code Editor:**

** Contribute your code and comments through Disqus.**

**Previous:**Write a Java program which reads a text (only alphabetical characters and spaces.) and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters.

**Next:** Write a Java program which reads the two adjoined sides and the diagonal of a parallelogram and check whether the parallelogram is a rectangle or a rhombus.

**What is the difficulty level of this exercise?**

Test your Programming skills with w3resource's quiz.

## Java: Tips of the Day

** How to remove leading zeros from alphanumeric text?**

Regex is the best tool for the job; what it should be depends on the problem specification. The following removes leading zeroes, but leaves one if necessary (i.e. it wouldn't just turn "0" to a blank string).

s.replaceFirst("^0+(?!$)", "")

The ^ anchor will make sure that the 0+ being matched is at the beginning of the input. The (?!$) negative lookahead ensures that not the entire string will be matched.

Test harness:

String[] in = { "01234", // "[1234]" "0001234a", // "[1234a]" "101234", // "[101234]" "000002829839", // "[2829839]" "0", // "[0]" "0000000", // "[0]" "0000009", // "[9]" "000000z", // "[z]" "000000.z", // "[.z]" }; for (String s : in) { System.out.println("[" + s.replaceFirst("^0+(?!$)", "") + "]"); }

Ref: https://bit.ly/2Qdcl8a

**Weekly Trends**- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises