-4

How to Perform operations on a single digit in a whole number? For example, if The input is "248" How do I make the program display the factorial of 8, Factorial of 4 and Factorial of 2? (Reversed)

Make it count the number of prime digits in the whole number

Make it count the number of digits divisible by 2 and 4 in the whole number

  • Check the question again. – Joshua Alwin Aug 25 '18 at 15:26
  • 1
    The stated question is: "How to Perform operations on a single digit in a whole number?" The duplicates proposed answer that. You have a couple of statements at the end - these are not questions. And if they were this would have been closed as "too broad". The information in the [help] explains how Stack Overflow works and how to effectively ask on-topic questions. One question per Question is the rule. And it's important to show what has been already tried and explain *how* it "doesn't work". – Cindy Meister Aug 25 '18 at 20:12

1 Answers1

1

You can convert the number to a string and then iterate over each character (digit):

def factorial(n):
    if n == 0:
        return 1
    return n * factorial(n-1)

num = 248
for digit in str(num):
    print(factorial(int(digit)))

You can't iterate over a number, but iterating over a string yields its individual characters. I had to convert the string version of the number's digits back into an integer for the factorial function to work.

N Chauhan
  • 3,407
  • 2
  • 7
  • 21