Site icon T4Tutorials.com

Factorial Program in C, C++ (C Plus Plus, CPP) with flow chart

Factorial Program in C, C++ (C Plus Plus, CPP) with flow chart

In this tutorial, we will learn about the followings;

  1. Flowchart of the factorial program
  2. C++ program for factorial program
  3. C program for factorial program

Logic of factorial

Suppose we want to calculate the factorial of 4, then we need to perform the multiplication in such a way as given below;

4*3*2*1=24, so factorial of 4 is 24.

Similarly, suppose we want to calculate the factorial of 5, then we need to perform the multiplication in such a way as given below;

5*4*3*2*1=120,  so factorial of 5 is 120.

Flowchart of the factorial program

Figure:
factorial flowchart with the program.

C++ program for factorial program

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i;
int num;
unsigned long long fact=1;
cout<<"Please enter a number: ";
cin>>num;
for(i=1;i<=num;i++)
{
fact=fact*i;
}
cout<<"Factorial of entered number is: "<<fact;
getch();
}

Output

Figure: Output of factorial program c.

SFT (Shamil’s Flow Table )

Are you interested to Read about SFT(Shamil’s Flow Table)?

Let us see the factorial program with Shamil’s program flow table.

Factorial Program in C++ using while loop

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int n,Factorial=1,loop=1;

    cout<<"\n Enter The Number:";
    cin>>n;
     
    //LOOP TO CALCULATE THE FACTORIAL OF A NUMBER
    while(loop<=n)
    {
        Factorial=Factorial*loop;
        loop++;
    }    
    cout<<"\n The Factorial of "<<n<<" is "<<Factorial;
    getch();
}

Output

Enter The Number:4

The Factorial of 4 is 24

Factorial Program in C++ using do while loop

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int loop=1,Number,factorial=1;
cout<<" Enter Any Number: ";
cin>>Number;
do
{
factorial=factorial*loop;
loop++;
}while(loop<=Number);
cout<<"\n factorialorial of given Number is: "<<factorial;
getch();
}

Output

Output

Enter Any Number: 5

Factorial of given Number is: 120

C++ Factorial Program using Recursion

#include<iostream>
using namespace std;

int factorial(int MyNumber);

int main() {

  int MyNumber;

  cout << "Enter a positive integer: ";
  cin >> MyNumber;

  cout << "Factorial of " << MyNumber << " = " << factorial(MyNumber);

  return 0;
}

int factorial(int MyNumber) {
  if(MyNumber > 1)
    return MyNumber * factorial(MyNumber - 1);
  else
    return 1;
}

Factorial Program in C++ using user define function

#include<iostream>
#include<conio.h>

using namespace std;

//Function
long T4Tutorials(int);

int main() {

    // Variable Declaration
    int LoopCounter, n;

    // Get Input Value
    cout << "Please Enter the Number :";
    cin>>n;

    cout << n << " T4Tutorials Value Is " << T4Tutorials(n);

    // Wait For Output Screen
    getch();
    return 0;
}

long T4Tutorials(int n) {
    int LoopCounter;
    long fact = 1;

    //for Loop Block
    for (int LoopCounter = 1; LoopCounter <= n; LoopCounter++) {
        fact = fact * LoopCounter;
    }

    return fact;
}

Output

Please Enter the Number :4
4 The Value Is 24

C program for factorial program

#include <stdio.h>
#include<conio.h>
int main()
{
    int i;
    int num;
    unsigned long long fact = 1;
    printf("Please Enter a number: ");
     scanf("%d",&num);
        for(i=1; i<=num;i++)
        {
            fact *= i;           
        }
        printf("Factorial of Entered number is %llu", fact);
    
getche();
}

 

Figure: Output of factorial program c.

More Practice on Factorial problem in C++

  1. Factorial Program in C++
  2. factorial using single inheritance
  3. Factorial Program in C++ using Class Objects
  4. factorial using Multiple inheritances
  5. C++ program for factorial using Constructor DestructorFactorial Of A Number By Using The Recursion
  6. Factorial Program with structures and pointers C++
  7. Factorial Program with Nested Structure C++
  8. factorial of a no. by defining the member functions outside the class
Exit mobile version