java Program to demonstrate the use of recursion to find the factorial of a given number.
import java.util.Scanner;
class Factorial
{
public static void main(String args[])
{
int n, c, f = 1;
System.out.println("Enter number");
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++)
f = f*c;
System.out.println("Factorial of "+n+" is = "+f);
}
}
}
output:
Enter number
9
Factorial of 9 is = 362880
تعليقات
إرسال تعليق
شاركنا الآراء ،،