views:

105

answers:

2

here is problem about sum of factorial of digit

http://projecteuler.net/index.php?section=problems&id=254

also here is Define sf(n) as the sum of the digits of f(n). So sf(342) = 3 + 2 = 5.

here is code which prints 32 as sum of digit's factorials but it does not show me second result

import java.util.*;
public class fact_of_digit1 {
public static int fact(int a){
    if (a==0)  return 1;
     return a*fact(a-1);
}
   public static int factorialofdigit(int a){
       int sum=0;
       int t=0;
       while (a!=0){
           t= a%10;
            a/=10;
            sum+=fact(t);
       }
       return sum;

   }
    public static void main(String[] args) {
       Scanner scnr=new Scanner (System.in);
       System.out.println("enter a:");
       int a=scnr.nextInt();
     System.out.println(factorialofdigit(a));
     System.out.println(sfn(a));
    }
    public static int sfn(int a){
        int sum1=0;
        int t=factorialofdigit(a);
         while (t!=0){
             sum1+=t%10;
             t/=10;



         }
         return sum1;
    }

}

result is:

enter a:
342
32
BUILD SUCCESSFUL (total time: 3 seconds)
A: 

The main function has two print statements which aren't in a loop. You will only get those two numbers printed.

btreat
"342" is the input. The output should be "32", and "5", which I get if I run it.
fgb
yes but i have not got it
A: 

The code looks fine, and works here, so it may be something to do with the IDE or how you're using it.

Are you running it debug mode? Did you set any breakpoints?

What happens if you create a new project and copy the code to it?

Does the following program work?

import java.util.*;
public class fact_of_digit1 {
    public static void main(String[] args) {
        Scanner scnr=new Scanner (System.in);
        System.out.println("enter a:");
        int a=scnr.nextInt();
        System.out.println(5);
        System.out.println(3);
    }
}

Replace the scanner with "int a = 342".

Add an extra println statement at the end.

fgb