Switch to full style
Java2 codes,problems ,discussions and solutions are here
Post a reply

Get Factorial using Java

Tue Apr 10, 2007 3:32 pm

This is the a small program using Java to get the Factorial of a integer number

example :
3!= 3 X 2 X 1 = 6


java code
/*


import java.util.Scanner;

/**
*
* @author mohamed
*/
public class Factor {

/** Creates a new instance of GCD */
public Factor() {
System.out.println("Factorial of number x! ");
Scanner inscan=new Scanner(System.in);
System.out.println("Please enter the number ");
a=inscan.nextInt();

System.out.println("("+a+") ! = "+get_Factor() );
}
public long get_Factor()
{
if(a<1)return 1;
long result=1;
long x=a;
while(x>1)
{
result*=x;

x--;
}
return result;
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Factor();
// TODO code application logic here
}

private int a;


}




Post a reply
  Related Posts  to : Get Factorial using Java
 Fibonacci vs factorial     -  
 recursively calculating factorial     -  
 factorial number Using Recursion     -  
 2d game in java-Monster-Java 2D Game Graphics and Animation     -  
 What is Java API?!!!     -  
 java or .net     -  
 Using FTP in java     -  
 what is java     -  
 Java course     -  
 need help in java     -  

Topic Tags

Java Algorithms