-1

so I'm back with a new question which is the following: I need to calculate the factorial of a user-input, now my problem is that I can't find any code nor explanation on how to do something like this, I also saw a topic on Stackoverflow but it didn't help me any further and I don't know where to start the only thing I have is the following:

  public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String invoer;
        System.out.print("Fill in a Number:");
        invoer = br.readLine();
}

so that's not much, I hope u guys can help me out!

Kind Regards, Mikey

Artifacialic
  • 51
  • 1
  • 2
  • 9
  • 1
    What do you mean by a 'faculty'? – Joe Clay Oct 25 '16 at 10:23
  • I'm sorry I meant the factorial – Artifacialic Oct 25 '16 at 10:25
  • calculate the faculty of a user-input what does it means ? – Dipak Thoke Oct 25 '16 at 10:25
  • It's not clear what are you asking. Do you know how to calculate a factorial number with a calculator or with pen and paper, or is it that you don't know how to calculate it programmatically? Your code doesn't show that much effort to be honest. Have you tried anything and got some errors, or have you not tried anything else at all? There's also 300000 results on google searching for _java factorial number_. Plenty of them from StackOverflow itself: http://stackoverflow.com/questions/8183426/factorial-using-recursion-in-java – José Luis Oct 25 '16 at 12:16
  • 1
    Possible duplicate of [Is there a method that calculates a factorial in Java?](http://stackoverflow.com/questions/891031/is-there-a-method-that-calculates-a-factorial-in-java) – José Luis Oct 25 '16 at 12:17

4 Answers4

2

Code which is use to calculate factorial given input number from the user:

import java.util.Scanner;

class Factorial
{
   public static void main(String args[])
   {
      int n, c, fact = 1;

      System.out.println("Enter an integer to calculate it's factorial");
      Scanner in = new Scanner(System.in);

      n = in.nextInt();

      if ( n < 0 )
         System.out.println("Number should be non-negative.");
      else
      {
         for ( c = 1 ; c <= n ; c++ )
            fact = fact*c;

         System.out.println("Factorial of "+n+" is = "+fact);
      }
   }
}

OR In more modular way

import java.util.Scanner;

public class Factorial {

   public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.print("Enter the number whose factorial is to be found: ");
       int n = scanner.nextInt();
       int result = factorial(n);
       System.out.println("The factorial of " + n + " is " + result);
   }

   public static int factorial(int n) {
       int result = 1;
       for (int i = 1; i <= n; i++) {
           result = result * i;
       }
       return result;
   }
}
Dipak Thoke
  • 1,963
  • 11
  • 18
1
static int factorial(int n){    
    return n == 0 ? 1 : (n * factorial(n-1));    
}    
Atul Kumar
  • 421
  • 3
  • 12
1
public class Test {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter No : ");
        int n=sc.nextInt();
        int fact=1;
        if(n>0)
        {
            for(int i=1;i<=n;i++)
                fact *= i;

            System.out.println("Factorial of "+n +" is = "+fact);
        }
        else
        {
            System.out.println("Invalid Input");
        }
}
}
Keval Pithva
  • 600
  • 2
  • 5
  • 21
0

This looks like a homework to me. You could decompose this problem to the following problems, and solve them in this order:

  1. Write a function that calculates the factorial of an int. Deal with edge cases, e.g. when n < 1. Invoke it from main with various inputs.
  2. Read the number from the Standard input. You can probably use a Scanner.
  3. Write the result to standard out with Sytem.out.println().
Tamas Rev
  • 7,008
  • 5
  • 32
  • 49