Algorithm Factorial in c, python, java and flowchart
Factorial
Q) Write an algorithm and draw the flowchart to calculate the factorial of a number.
Table of Contents
what is a factorial
Finding the product of all positive numbers from 1 to a certain nonnegative integer is done mathematically using a factorial. The exclamation symbol (!) is used to indicate it. The factorial, for instance, of a positive integer βnβ is denoted by the symbol βn!β and is computed as follows:
n! = n Γ (n1) Γ (n2) Γ β¦ Γ 3 Γ 2 Γ 1
For instance:
 5! = 5 Γ 4 Γ 3 Γ 2 Γ 1 = 120
 4! = 4 Γ 3 Γ 2 Γ 1 = 24
 1! = 1 (by convention, the factorial of 1 is defined to be 1)
In many mathematical and statistical procedures, including permutations, combinations, and probability distributions, factories are utilized. They are also used in fields including engineering, physics, and computer science.
Algorithm Factorial
 This is a flowchart to calculate the factorial of the given number.
 nAccepted number
 factFactorial of number
 i β loop variable,
Algorithm Factorial Steps
step 1: Start
step 2: [Accepte a number] Read n
step 3: set i=1, fact1
step 4: Repeat steps 5 and 6 until i <= n
step 5: [calcute factorial] set fact=fact*i
step 6: set i=i+1
step 7: [ display factorial ] Print fact
step 8: Stop
Factorial Flowchart
An illustration of how to calculate the factorial of a given positive integer using a flowchart:
 The flowchart starts right here.
 βnβ should be a positive integer: βnβ, a positive number, must be entered when the user is requested.
 Placing βfactorialβ at 1: βfactorialβ is a variable with a starting value of 1. The result of the last factorial will be kept in this variable.
 Put βcurrentβ at 1: βcurrentβ is a variable with a starting value of 1. The current integer will be tracked by this variable when the multiplication procedure is performed.
 Do the following when βcurrentβ is less than or equal to βnβ: The loop in question will keep running until βcurrentβ exceeds βn.β
 Factorial multiplied by current: The βfactorialβ is multiplied by the current value of βcurrentβ throughout each iteration of the loop.
 βcurrentβ is increased by 1 to represent the change from one integer to the next after each iteration.
 End While: The loop terminates when βcurrentβ exceeds βn.β
 As a consequence, print the value of βfactorialβ: The factorial of the input integer βnβ is the end value of βfactorial.β
 The flowchart comes to an end here.
The flowchart shows a straightforward approach that uses a loop and a few variables to keep track of the progress to compute the factorial of a given positive integer.
Factorial Flowchart steps
Start
β
Input a positive integer 'n'
β
Set 'factorial' to 1
β
Set 'current' to 1
β
While 'current' is less than or equal to 'n', do the following:

 Multiply 'factorial' by 'current'
 β
 'factorial' = 'factorial' Γ 'current'
 β
 Increment 'current' by 1
 β
End While

Output the value of 'factorial' as the result
β
End
what is the factorial of hundred
The result of calculating the factorial of 100 (100!) would be a very huge number. Due to its enormous magnitude, it is essentially impossible to write the actual figure here. The factorial of 100 is 158 digits long and roughly equals:
100! β 9.33262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000000
Itβs a huge number with 24 trailing zeros, as you can see. Factorials increase exponentially, making it difficult to type down the complete result for bigger quantities. Although it is frequently used in a variety of academic and theoretical debates, the factorial of 100 is not a realistic number to compute and deal with in the majority of realworld situations.
Factorial in c
A simple C program to figure out the factorial of an input positive integer
#include <stdio.h>
// Function to calculate the factorial
unsigned long long factorial(int n) {
if (n == 0  n == 1)
return 1;
else
return n * factorial(n  1);
}
int main() {
int num;
printf("Enter a positive integer: ");
scanf("%d", &num);
if (num < 0) {
printf("Error: Factorial is not defined for negative numbers.\n");
} else {
unsigned long long result = factorial(num);
printf("Factorial of %d is %llu.\n", num, result);
}
return 0;
}
Factorial program in c
The factorial of the provided positive integer num is calculated by this program using a recursive function called factorial. As the factorial is not specified for negative values, it checks to see whether the input is negative and shows an error notice if it is.
The printf function is used by the program to compute and print the factorial result for nonnegative inputs. Since factorials grow quickly and can exceed the range of conventional integer types, handling high factorial values requires the usage of the unsigned long long data type.
Factorial in C++
An application in C++ that computes the factorial of a given positive integer
#include <iostream>
// Function to calculate the factorial
unsigned long long factorial(int n) {
if (n == 0  n == 1)
return 1;
else
return n * factorial(n  1);
}
int main() {
int num;
std::cout << "Enter a positive integer: ";
std::cin >> num;
if (num < 0) {
std::cout << "Error: Factorial is not defined for negative numbers.\n";
} else {
unsigned long long result = factorial(num);
std::cout << "Factorial of " << num << " is " << result << ".\n";
}
return 0;
}
Factorial program in C++
The C++ program and the C program are pretty similar. It determines the factorial of the supplied positive integer num using the recursive function factorial. If the input is negative, the software verifies it and shows an error message.
The program computes the factorial result for nonnegative inputs, prints it, and displays the result using the cout stream object. To handle big factorial numbers, utilize the unsigned long long data type.
Factorial in Python
The factorial of a given positive integer using a Python function
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n  1)
try:
num = int(input("Enter a positive integer: "))
if num < 0:
print("Error: Factorial is not defined for negative numbers.")
else:
result = factorial(num)
print(f"Factorial of {num} is {result}.")
except ValueError:
print("Error: Invalid input. Please enter a valid positive integer.")
Factorial program in Python
The factorial of the provided positive integer num may be calculated using the recursive function factorial defined by this Python program. A tryexcept block is used to capture any noninteger inputs and gently show an error message, taking care of managing erroneous input.
If the input is negative, the software verifies it and shows an error message. The program computes the factorial result for nonnegative inputs, prints it, and displays the result using the print() method.
Factorial in Java
A Java application that figures out the factorial of an input positive integer
import java.util.Scanner;
public class FactorialProgram {
// Function to calculate the factorial
public static long factorial(int n) {
if (n == 0  n == 1)
return 1;
else
return n * factorial(n  1);
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int num = input.nextInt();
if (num < 0) {
System.out.println("Error: Factorial is not defined for negative numbers.");
} else {
long result = factorial(num);
System.out.println("Factorial of " + num + " is " + result + ".");
}
input.close();
}
}
Factorial program in Java
To compute the factorial of the provided positive integer num, we define a static method factorial in this Java application. To read user input from the terminal, we make use of the Scanner class. If the input is negative, the software verifies it and shows an error message.
The application computes the factorial result for nonnegative inputs and prints it using the System.out.println() function. Large factorial numbers are handled via the long data type.