import java.util.Scanner;
public class Factorial
{
public int factorial(int n)
{ if(n==1)
{
return 1;
}
while(n>1)
{
return(n*factorial(n-1));
}
}
public static void main(String[] args)
{ int fact=1;
Scanner sc=new Scanner(System.in);
System.out.println("Enter Number:");
int n=sc.nextInt();
Factorial f=new Factorial();
fact=f.factorial(n);
System.out.println("Factorial is:"+fact);
}
}
//output:
Enter Number:
4
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
This method must return a result of type int
at Factorial.factorial(Factorial.java:5)
at Factorial.main(Factorial.java:23)
Your paths are not all covered by return;
while(n>1)
{
return(n*factorial(n-1));
}
return 1; // Add this