Python: Find the number of combinations of a,b,c and d
Python Basic - 1: Exercise-37 with Solution
Write a Python program that 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) will be equal to n.
n (1 ≤ n ≤ 50)
Input the number(n): 15
Number of combinations: 592
import itertools print("Input the number(n):") n=int(input()) result=0 for (i,j,k) in itertools.product(range(10),range(10),range(10)): result+=(0<=n-(i+j+k)<=9) print("Number of combinations:",result)
Input the number(n): 15 Number of combinations: 592
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program to compute the amount of the debt in n months. The borrowing amount is $100,000 and the loan adds 5% interest of the debt and rounds it to the nearest 1,000 above month by month.
Next: Write a Python program to print the number of prime numbers which are less than or equal to an 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
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join