F JAVA Program To Calculate Factorial Of A Number | CodeTheta

JAVA Program To Calculate Factorial Of A Number

August 09, 2016

Using predefined value
In this predefined program as you can see that i have already defined the value in InputNumber variable and the value is 3.

Code :
class Factorial
{
 public static void main(String args[])
 {
  int InputNumber=3,LoopVariable,Result=1;
  for (LoopVariable=1;LoopVariable<=InputNumber;LoopVariable++)
  {
   Result=Result*LoopVariable;
  }
  System.out.println("Factorial Result is: "+Result);
 }
}

Output :
Factorial is: 6


Using user input
Here you can see that i have used Scanner class to take the value from the user and calculate the factorial.

Code :
import java.util.Scanner;
class Factorial
{
 public static void main(String args[])
 {
  int InputNumber,LoopVariable,Result=1;
  Scanner ScannerObject = new Scanner(System.in);
  System.out.println("Enter a Positive Number To Calculate Its Factorial: ");
  InputNumber = ScannerObject.nextInt();
  for (LoopVariable=1;LoopVariable<=InputNumber;LoopVariable++)
  {
   Result=Result*LoopVariable;
  }
  System.out.println("Factorial Result is: "+Result);
 }
}

Output :
Enter a Positive Number To Calculate Its Factorial:
5
Factorial Result is: 120

IDE Used To Test This Code : Dr Java

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Post a Comment

PRODUCTS

LISTS OF PRODUCTS

SERVICES

SKILLS
SOFTWARE DEVELOPMENT
WEBSITE DEVELOPMENT
WEB HOSTING
BULK SMS SERVICE
SEO SERVICE
ANDROID APPS
QR CODE / BARCODE
HARDWARE SERVICE
OUR WORK AREA