Table of Contents
Top 3 Way to Find Product of digits a Number in Python
Algorithm
Using While Loop
Using Recursion
Algorithm
#Write the Algorithm to find the Product of digits of the number.
- Start
- Let the number be x, Variable a for Reminder,r for result
- Initialize variable r=0
- Condition (x!=0)
- a=x%10
- r=r*a
- x=x/10 goto Step 4
- Display Result r
- Stop
Using While Loop
#Program to find Product of Digits of a Numbers using Using While loop
r=0
print("Enter the number")
n=int(input())
while n!=0:
a = n% 10
r=r* a
n=n//10
print("Product of Digits is: ",r)
Output:
Enter the number 567
Product of Digits is: 210
Product of Digits is: 210
Using Recursion
#Product of digits of a Number Using Recursion
n= int(input("Enter the Choice"))
r=0
def sum(n):
global r # We can use it out of the function
if (n > 0):
a= n % 10
r= r* a
sum(n // 10)
return r
r=sum(n)
print("Product of digits is =",r)
Output:
Enter the number 1213
Sum of Digits is: 6
Sum of Digits is: 6