Write a program to input a number n and print its factorial.
Answers
Answer:
C program to find factorial of a number
long factorial(int);
int main() { int n;
printf("Enter a number to calculate its factorial\n"); scanf("%d", &n);
printf("%d! = %ld\n", n, factorial(n));
return 0; }
long factorial(int n) { int c; long r = 1;
for (c = 1; c <= n; c++) r = r * c;
return r; }
Explanation:
HOPE IT HELPS.........
Answer:
Programming Language not mentioned...Done in JAVA and python
Java approach:-
//Program to print the factorial of given number
package Programmer;
import java.util.*;
public class Fact{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
System.out.println("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);
}
}
_____
Python approach:-
n,f=(int)(input("Enter a number")),1
for I in range(1,n+1):
f=f *I
print("Factorial of ",n," is",f)
_____
Variable Description:-
- f:- to calculate factorial of a number
- I:- loop variable
- n:- number ínput Variable