+ 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(); }
4 Antworten
+ 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();
    }
0
Thank You Satyam Mishra.
Fix that problem.😊
0
👍👍👍 User42



