Computer Science, asked by Anonymous, 5 months ago

WAP in JAVA to print factorial

Answers

Answered by darksoul3
3

Let's see the factorial Program using loop in java.

class FactorialExample{

public static void main(String args[]){

int i,fact=1;

int number=5;//It is the number to calculate factorial.

for(i=1;i<=number;i++){

fact=fact*i;

}

System.out.println("Factorial of "+number+" is: "+fact);

Answered by BrainlyProgrammer
3

Question:-

  • To print the factorial of a number

Code:-

import java.util.*;

class Fact

{

public static void main (String ar [])

{

int f=1,i;

System.out.println("Enter a number");

int n=sc.nextInt();

for(i=1;i<=n;i++)

{

f*=i;

}

System.out.println("Factorial of "+n+" is"+f);

}

}

Variable description:-

  • f :- to store factorial of a number
  • i :- loop variable
  • n:- input variable

Similar questions