All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Factorial Program In Java Using Scanner

Last Updated : Mar 11, 2024

Factorial Program In Java Using Scanner

In this article we will show you the solution of factorial program in java using scanner, as usual you need to import package 'Nextset', after to getting input from user imported scanner package.

We defined main class with the same name of program, which contains static main method. There we asking user to enter a number to finds out factorial for that number. Then you need to implement factorial code and printed factorial result on terminal.

Step By Step Guide On Factorial Program In Java Using Scanner :-

Within class 'fact_prgm' we defined main method, there requesting user to enter number, which is factorial they need.

Then we doing factorial process with the help of loop, inside you have to multiply each values until reach the input value to get a factorial for that input. At last we printed result on terminal. You can give any integer datatype number that factorial will display accurately.

//factorial program with scanner
package Nextset;
import java.util.Scanner;
class fact_prgm
{
    public static void main(String args[])
    {
     Scanner sobj=new Scanner(System.in);
        System.out.println("Enter a number you will get thats factorial");
        int inp=sobj.nextInt();
        int i,f=1;
        for(i=1;i<=inp;i++){
         f=f*i;
        }
        System.out.println(f);
    }
}
  1. The ‘Package Nextset’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces. The util.Scanner widely supports to getting input from end user.
  2. The ‘public static void main(String[] args)’ is defined that's starting point from where compiler starts program execution.
  3. At first we defined main class 'fact_prgm', then written public static void main() method. There foremost we created scanner object 'sobj', then printed text ' Enter a number you will get thats factorial'.
  4. To instruct user to get input for finds out factorial, the user input stored on variable 'inp'. Which is done by appending 'nextInt' property with scanner object 'sobj'.
  5. We declared variables 'i,f' to proceed further processes, and variable 'f' initilized with value '1'. Within for loop for iteration we using variable 'i', which is initialized with value '1'.
  6. This loop will continue until it reaches condition 'i<=inp', that denotes user input value. Within loop we starts the multiplication operation from 1 to til user input. Then you can get factorial output that is again stored on variable 'f' and at last we printed on terminal.
  7. The ‘System.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  8. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to calculate factorial for given number in java.

To compile java program you need to tell the java compiler which program to compile through ‘javac fact_prgm.java’.

After successful compilation you need to execute ‘java fact_prgm‘line for gets the output.

Then you can see text 'Enter a number you will get thats factorial' on terminal. Now user need to provide any integer input, For example user gave 5 as input then you will get result is 120.

On eclipse just press of run button then you will get output inside terminal.

I hope this article on factorial program in java using scanner helps you and the steps and method mentioned above are easy to follow and implement.