मराठी

Write a program to enter a number and print the sum of factorial of the digits.

Advertisements
Advertisements

प्रश्न

Write a program to enter a number and print the sum of factorial of the digits.

कोड लेखन
Advertisements

उत्तर

import java.util.*;
class clDigits
{
    void main()
    {
        Scanner sc = new Scanner(System.in);
        int N, d, fd, sum = 0;        
        System.out.print("Enter a number : ");
        N = sc.nextInt();        
        int cn = N;
        while(cn > 0)
        {
            d = cn % 10;
            fd = 1;            
            for(int k = 1; k <= d; k++)
            {
                fd = fd * k;
            }            
            sum = sum + fd;
            cn = cn / 10;
        }        
        System.out.println("Sum of factorial of digits of " + N + " : " + sum);
    }
}

Output: 

Enter a number : 254

Sum of factorial of digits of 254: 146

shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 11: Programs Overall - Digit Handling [पृष्ठ २५२]

APPEARS IN

रूपा पंडित Computer Applications [English] Class 10 ICSE
पाठ 11 Programs Overall
Digit Handling | Q 7. | पृष्ठ २५२
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×