This Post explains the concept of factorial numbers and to find the factorial of the number Programming languages are used. In this post, four programming languages (C, C++, Java & Python) are used to find the factorial of a number.
What is Factorial ?
The multiplication of all positive integers less than or equal to the number is known as the factorial of the number.
For Example: Factorial of 7 is 7 x 6 x 5 x 4 x 3 x 2 x 1 = 5040
With the help of programming languages, we can easily find out the factorial by using different ways. Some of the implementation of the programming language to find the factorial is described below. To understand the code describe below we need to know the following concepts:
For Loop:
For loop, all its loop gathered elements are gathered at one place. The general form of the loop is i.e. syntax of For Loop For C, C++, Java & Python:
For(initialization-expression ; test-expression ; update-expression)
{
Body of the Loop;
}
If Else Statement:
If Else statement is either- or condition in which only one statement is true depending on the condition used. Syntax of If-Else for C, C++ & Java:
If (expression)
Statement 1;
else
Statement 2;
For Python syntax is
If (expression)
:
Statement 1;
else :
Statement 2;
Else if Statement
In the else-if statement, we can check more than one condition in the same code. Syntax of else If for C, C++ & Java
If (expression)
Statement 1;
else
if (expression)
Statement 2;
else
statement 3;
For Python syntax is
If (expression)
:
Statement 1;
elif (condition):
Statement 2;
else
statement 3;
Recursion:
The Function that call itself in the execution is known as recursion.
Factorial of a Number in C
Code of the Factorial in C:
#include<stdio.h>
int main()
{
int i,fact=1,num;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",num,fact);
return 0;
}
Output:
Enter a number: 5
Factorial of 5 is 120
Factorial of a Number in C Using Recursion
Code of the Factorial in C Using Recursion:
#include<stdio.h>
int factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
int main()
{
int num,fact;
printf("Enter a number: ");
scanf("%d", &num);
fact = factorial(num);
printf("Factorial of %d is %d\n", num, fact);
return 0;
}
Output:
Enter a number: 7
Factorial of 7 is 5040
Factorial of a Number in C++
Code of the Factorial in C++:
#include<iostream>
using namespace std;
int main()
{
int i,fact=1,num;
cout<<"Enter a Number: ";
cin>>num;
for(i=1;i<=num;i++){
fact=fact*i;
}
cout<<"Factorial of " <<num<<" is: "<<fact<<endl;
return 0;
}
Output:
Enter a Number:9
Factorial of 9 is: 362880
Factorial Program in Java
Code of the Factorial in Java:
import java.util.Scanner;
class Factorial{
public static void main(String args[]){
int i,fact=1;
Scanner sc = new Scanner(System.in);
System.out.print("enter the number to calculate factorial");
int num = sc.nextInt();
for(i=1;i<=num;i++){
fact=fact*i;
}
System.out.println("Factorial of "+num+" is: "+fact);
}}
Output:
enter the number to calculate factorial 8
Factorial of 8 is :40320
Factorial Program in Python
Code of the Factorial in Python:
num = int(input("Enter a number: "))
fact = 1
if num < 0:
print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,num + 1):
fact = fact*i
print("The factorial of",num,"is",fact)
Output:
Enter a number:4
The factorial of 4 is 24
Discover more from easytechnotes
Subscribe to get the latest posts sent to your email.
Good job..
Nice work
Keep going on
Keep working on it…very much helpfull site✌✌
Keep working loved your work
thanks