Sum of Factorials of the digits of N.
Sum of Factorial of the digits of the given number: Write a python program to compute sum of
the factorials of each digit of a given integer. Write a separate function for calculating the
factorial of a number.
the factorials of each digit of a given integer. Write a separate function for calculating the
factorial of a number.
def fact(n): result = 1 for i in range(n): result *= i + 1 return result def sum_of_fact(n): result = 0 if(n <= 0): return 1 else: while n > 0: result += fact(n % 10) n = n // 10 return result print(sum_of_fact(int(input())))
import java.util.Scanner; public class Solution { public static long factorialN(long n) { if (n <= 0) return 1; else return n * factorialN(n-1); } public static void main(String[] args) { Scanner scan = new Scanner(System.in); long n = scan.nextLong(); int sum = 0; if(n <= 0) { System.out.println("1"); } else { while(n != 0) { sum = sum + (int)factorialN(n%10); n = n/10; } System.out.println(sum); } } }C#include <stdio.h> long long int factorialN(long long int n) { if (n <= 0) return 1; else return n * factorialN(n-1); } int main(int argc, char const *argv[]) { long long int n = 0; long long int sum = 0; scanf("%llu",&n); if(n <= 0) { printf("1"); } else { while(n != 0) { sum = sum + factorialN(n%10); n = n/10; } printf("%llu\n",sum); } return 0; }
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment