answersLogoWhite

0

import java.math.BigInteger;

public class Factorial {

public static void main(String[] args) {

BigInteger n = BigInteger.ONE;

for (int i=1; i<=20; i++) {

n = n.multiply(BigInteger.valueOf(i));

System.out.println(i + "! = " + n);

}

User Avatar

Wiki User

14y ago

Still curious? Ask our experts.

Chat with our AI personalities

BeauBeau
You're doing better than you think!
Chat with Beau
MaxineMaxine
I respect you enough to keep it real.
Chat with Maxine
DevinDevin
I've poured enough drinks to know that people don't always want advice—they just want to talk.
Chat with Devin
More answers

import java.io.*;

class Factorial_For{

public static void main(String[] args) {

try{

BufferedReader object = new BufferedReader(new InputStreamReader(System.in));

System.out.println("enter the number");

int a= Integer.parseInt(object.readLine());

int fact= 1;

System.out.println("Factorial of " +a+ ":");

for (int i= 1; i<=a; i++){

fact=fact*i;

}

System.out.println(fact);

}

catch (Exception e){}

}

}

User Avatar

Wiki User

14y ago
User Avatar

Recursion is not the most efficient in this case; but this serves to demostrate the basic principles of recursion. Recursion is really useful in some other cases; it can make problems that otherwise look impossible, to actually seem easy, once you grasp the basic ideas of recursion. 5! (5 factorial), for example, is 1 x 2 x 3 x 4 x 5. This can also be defined as 5 x 4! (5 times 4 factorial). For a useful recursion, there must be an ending condition; in this case, 0! is defined as 1. The Java function looks something like this: int factorial(int number) { if (number == 0) return 1; else return number * factorial(number - 1); } The following is a shorter, but equivalent, version that uses the ternary operator: int factorial(int number) { return number == 0 ? 1 : number * factorial(number - 1); }

Recursion is not the most efficient in this case; but this serves to demostrate the basic principles of recursion. Recursion is really useful in some other cases; it can make problems that otherwise look impossible, to actually seem easy, once you grasp the basic ideas of recursion. 5! (5 factorial), for example, is 1 x 2 x 3 x 4 x 5. This can also be defined as 5 x 4! (5 times 4 factorial). For a useful recursion, there must be an ending condition; in this case, 0! is defined as 1. The Java function looks something like this: int factorial(int number) { if (number == 0) return 1; else return number * factorial(number - 1); } The following is a shorter, but equivalent, version that uses the ternary operator: int factorial(int number) { return number == 0 ? 1 : number * factorial(number - 1); }

Recursion is not the most efficient in this case; but this serves to demostrate the basic principles of recursion. Recursion is really useful in some other cases; it can make problems that otherwise look impossible, to actually seem easy, once you grasp the basic ideas of recursion. 5! (5 factorial), for example, is 1 x 2 x 3 x 4 x 5. This can also be defined as 5 x 4! (5 times 4 factorial). For a useful recursion, there must be an ending condition; in this case, 0! is defined as 1. The Java function looks something like this: int factorial(int number) { if (number == 0) return 1; else return number * factorial(number - 1); } The following is a shorter, but equivalent, version that uses the ternary operator: int factorial(int number) { return number == 0 ? 1 : number * factorial(number - 1); }

Recursion is not the most efficient in this case; but this serves to demostrate the basic principles of recursion. Recursion is really useful in some other cases; it can make problems that otherwise look impossible, to actually seem easy, once you grasp the basic ideas of recursion. 5! (5 factorial), for example, is 1 x 2 x 3 x 4 x 5. This can also be defined as 5 x 4! (5 times 4 factorial). For a useful recursion, there must be an ending condition; in this case, 0! is defined as 1. The Java function looks something like this: int factorial(int number) { if (number == 0) return 1; else return number * factorial(number - 1); } The following is a shorter, but equivalent, version that uses the ternary operator: int factorial(int number) { return number == 0 ? 1 : number * factorial(number - 1); }

User Avatar

Wiki User

15y ago
User Avatar

Recursion is not the most efficient in this case; but this serves to demostrate the basic principles of recursion. Recursion is really useful in some other cases; it can make problems that otherwise look impossible, to actually seem easy, once you grasp the basic ideas of recursion. 5! (5 factorial), for example, is 1 x 2 x 3 x 4 x 5. This can also be defined as 5 x 4! (5 times 4 factorial). For a useful recursion, there must be an ending condition; in this case, 0! is defined as 1. The Java function looks something like this: int factorial(int number) { if (number == 0) return 1; else return number * factorial(number - 1); } The following is a shorter, but equivalent, version that uses the ternary operator: int factorial(int number) { return number == 0 ? 1 : number * factorial(number - 1); }

User Avatar

Wiki User

15y ago
User Avatar

teri maa ka booshda

User Avatar

Wiki User

12y ago
User Avatar

Add your answer:

Earn +20 pts
Q: 2 Write a program in java to find factorial of a number?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Write the Pseudocode to find the factorial of a number?

Pseudo code+factorial


Using while loop Write a program find the factors of a number?

by this program you can find the factorial: #include&lt;iostream&gt; using namespace std; main() { int n,x,f=1; cin&gt;&gt; n; x=0; while(x&lt;n) { x++; f= f*x; } cout&lt;&lt;"factorial is"&lt;&lt;f&lt;&lt;"\n"; system("pause"); return 0; }


Program to find factorial of a number using inheritance?

/*71.PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION*/ #include&lt;stdio.h&gt; #include&lt;conio.h&gt; int fact(int); void main() { int n,f; clrscr(); printf("Enter number whose factorial is to be calculated: "); scanf("%d",&amp;n); if(n&gt;0) { f=fact(n); printf("factorial of %d is %d",n,f); } else printf("Factorial of numbers less than 1 does not exist"); getch(); } int fact(int n) { int facto=1; if(n&gt;1) facto=n*fact(n-1); else return 1; return(facto); }


How do you create factorial program in qbasic?

since factorial is for example , the factorial of 5 = 5 (5-1)(5-2)(5-3)(5-4) that means the last number to subtract from 5 is 4 , which is (n-1) ie the factorial of any number is (n-0)(.............)(n-(n-1)) to write this , 5 REM to calculate the factorial of any number 6 DIM fac AS INTEGER LET fac = 1 10 INPUT "enter the number to find its factorial "; a ' variable a 15 FOR b = 0 TO (a-1) 'numbers that will be subtracted from the " a" 20 c= a -b 'each number in the factorial calculation 25 fac = fac * c 'to compute each multiplication in the factorial 30 NEXT b 35 PRINT 'to leave a line 40 PRINT fac 45 END note this due to some unattained raesons works for numbers 0 to 7


How to write a program to find the delimiter matching in stacks?

== == using recursions: unsigned int Factorial( unsigned int x) { if(x&gt;0) { return ( x * Factorial(x-1)); } else { return(1); } } factorial: unsigned int Factorial( unsigned int x) { unsigned int u32fact = 1; if( x == 0) { return(1); } else { while(x&gt;0) { u32fact = u32fact *x; x--; } } }