+ 2

How to slove this problem?

package first; import java.util.Scanner; public class fac { static double fact(double a) { double fact=1, i; if(a==0) { return fact; } else { for(i=1;i<=a;i++) { fact=fact*i; } return fact; } } public static void main(String[] args) { double a,fact; Scanner input = new Scanner(System.in); System.out.println("Enter a number to find its factorial : "); a = input.nextInt(); fact=fact(a); System.out.println(a+" ! = "+fact); } input.close(); }

17th Jun 2022, 3:22 AM
Anuradha Dilshan
Anuradha Dilshan - avatar
4 Answers
+ 1
There are few small mistake such as in main method input.close(); should be inside method and then a is declared double datatype but while taking input you used nextInt() which might raise error. Change your main method with this. public static void main(String[] args) { double a, fact; Scanner input = new Scanner(System.in); System.out.println("Enter a number to find its factorial : "); a = input.nextDouble(); fact = fact(a); System.out.println(a+" ! = "+fact); input.close(); }
17th Jun 2022, 4:31 AM
Satyam Mishra
Satyam Mishra - avatar
0
Thank You Satyam Mishra. Fix that problem.😊
17th Jun 2022, 4:33 AM
Anuradha Dilshan
Anuradha Dilshan - avatar
0
👍👍👍 User42
17th Jun 2022, 4:33 AM
Anuradha Dilshan
Anuradha Dilshan - avatar