w3resource

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.

Input:
n (1 ≤ n ≤ 50)
Input the number(n): 15
Number of combinations: 592

Sample Solution:

Python Code:

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)

Sample Output:

Input the number(n):
 15
Number of combinations: 592

Flowchart:

Flowchart: Python - Find the number of combinations of a,b,c and d

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.



Follow us on Facebook and Twitter for latest update.