Java: Reads an integer n and find the number of combinations of a,b,c and d will be equal to n
Java Basic: Exercise-216 with Solution
Write a Java program which reads an integer n and finds the number of combinations of a,b,c and d (0 ≤ a,b,c,d ≤ 9) where (a + b + c + d) equals n.
Input:
n (1 ≤ n ≤ 50).
Sample Solution:
Java Code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("Input the number(n):");
Scanner s = new Scanner(System.in);
int c = s.nextInt();
int ans = check(c);
System.out.println("Number of combinations of a, b, c and d :");
System.out.println(ans);
}
static int check(int c) {
int ctr = 0;
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
for(int k = 0; k < 10; k++) {
for(int l = 0; l < 10; l++) {
if(i + j + k + l == c) {
ctr++;
}
}
}
}
}
return ctr;
}
}
Sample Output:
Input the number(n): 5 Number of combinations of a, b, c and d : 56
Flowchart:

Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Write a Java program to compute the amount of the debt in n months. The borrowing amount is $100,000 and the loan adds 4% interest of the debt and rounds it to the nearest 1,000 above month by month.
Next: Write a Java program to print the number of prime numbers which are less than or equal to a given integer.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- 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