WAP to take a number as input and find its factorial .
pls help me out ... don't spam
Answers
Answered by
9
Attachments:
Answered by
16
Answer:
//Program to print the factorial of a number
package Programmer;
import java.util.*;
public class Fact{
public static void main (String ar[]){
Scanner sc=new Scanner (System.in);
System.out.print("Enter a number");
int n=sc.nextInt();
int f=1;
for(int i=1;i<=n;i++){
f *=i;
}
System.out.println("Factorial of "+n+" is "+f);
}
}
_____
Variable Description:-
- f:- To calculate and store factorial of a number
- n:- to accept number as Input from the user
_____
Explaination:-
- The program accepts the number from the user and stores it in variable 'n'.
- variable 'f' is initialized to 1
- Then I loop is running from 1 to n
- f gets multiplied with loop value(value of I)
- loop works till it's value is less than or equal to value of variable 'n'
- Finally loop terminates when condition is false(I<=n)
- Factorial of a number gets printed
_____
•Output Attached
Attachments:
Similar questions