How to write a FACTORIAL PROGRAM in Java using the for loop
import java.util.Scanner;
import java.lang.Math;
public class hay {
public static void main( String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("enter The no : ");
int n=obj.nextInt();
int ans=1;
if(n==0) {
ans=1;
System.out.println(ans);
}
else
{
for(int i=1;i<=n;i++)
{
ans=ans*i;
}
System.out.println(ans);
}
Comments
Post a Comment