Program to find factorial of any no

import java.util.Scanner;
public class factorial_of_any_no
{
public static void main(String args[])
{
int fa=1;
Scanner sc =new Scanner(System.in);
System.out.println("enter the no");
int n=sc.nextInt();
if(n<0)
System.out.println("not defined");
else
{
for(int i=1;i<=n;i++)
{
fa=fa*i;
}
System.out.println(fa);
}
}
}

Another method

import java.util.Scanner;
class Factorial_In_Dec_Way
{
public static void main (String args[])
{
Scanner define =new Scanner(System.in);
System.out.println("enter the no for factorial");
int f = define.nextInt();
if(f<0)
{
System.out.println("not defined");
}
else
{
for (int i=(f-1); i>=1; i--)                        //recuursion with function call
{
f=f*i;
}
System.out.println(f);
}
}
}

No comments:

Post a Comment