Write a program in C++ & python to calculate the Factorial of an Integer

By | July 1, 2019

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

Leave a Reply

Your email address will not be published. Required fields are marked *