To find a factorial of a given number is very simple. For exp if we have to find factorial of 5 than we have to (5*4*3*2*1) here we are doing multipication of a number and print sum of it.
import java.util.Scanner; public class factorial { public static void main(String[] args) { Scanner sc= new Scanner(System.in); System.out.println("enter the number: "); int num=sc.nextInt(); int sum=1; for(int i=num;i>=1;i--) { sum=sum*i; if(i==1) { System.out.println("factorial of"+" "+num+" "+ "is-->" +" "+sum); } } } }
No comments:
Post a Comment