In this program, we will code to calculate the factorial of an integer.
Factorial is a math concept which has a! sign
For example, 3 factorials would be represented as 3! = 3*2*1
0! = 1
Factorial of an Integer
C++:
#include<iostream.h> #include< conio.h> #include<stdio.h> #include<math.h> void main() { clrscr(); int num,fact=1; cout<<"Enter a number: "; cin>>num; for(int i=1; i<=num; i++) fact *= i; if(num==0) cout<<"The factorial is 1"; else cout<<"The factorial of "<<num<<" is "<<fact; getch(); }
Output:
Enter a Number: 5 The factorial of 5 is 120
Python:
num =int(input("Enter a Number: ")) fact=1 for i in range(1,num+1): fact*=i if num==0: print("The factorial is 1") else: print("The factorial of",num,"is",fact)
Output:
Enter a Number: 6 The factorial of 6 is 720
People are also reading:
- WAP to swap two numbers
- WAP in C++ and python to convert temperature from Fahrenheit to Celsius and vice-versa
- WAP in C++ and python to check whether the year is a leap year or not
- Perfect Number in C
- WAP in C++ & Python to reverse a number
- WAP in C++ & Python to calculate the size of each data types
- WAP in C to Check Whether the Number is a Prime or not
- WAP to print the truth table for XY+Z
- C++ and Python Code to Find The Sum of Elements Above and Below The Main Diagonal of a Matrix
- Linear Search in Python and C++