Skip to main content
 

#WSQ09 #TC1017

2 min read

 

Factorial Calculator:

A Factorial calculator it´s defined as a calculation of the factorial. This is represented as the Multiplication of the composition of the number given. For example: If you want to calculate the factorial of 4! you should multiply 4*3*2*1=24. In a C++ programm it´s required to do a loop for making a programm that can calculate the factorial of a given number. The first thing required it´s making the structure of your programm, libraries (basic libraries), using namespace std; , int main ( ) { }, and then inside the main a loop indicating the start and the end of it it´s required. Notice that i has to be lower than 1 to define the work time of the loop. A new integer it´s required to save the result of the multiplications. On the example code it´s defined as factorial. Notice that the factorial it´s equal to the given number because the multiplication has to start with a number lower than the given number, example 4 (given number) 3 (begin of the multiplication so, the loop will do the follow equation: 4*3*2*1.

 

 

Example code:

<iostream>

using namespace std;

 

 

int main ()

{

  char A;

  do

  {

    int n,factorial;

 

    cout << "This program will display the factorial of a given number, Enjoy."<< endl;

    cout << "Give me the number: "<< endl;

    cin >> n;

 

    factorial=n;

    for (int i=n; i>1; i--)

    {

      factorial= factorial * i ;

    }

 

    cout << "The factorial of the given number is: " << factorial << endl;

    cout << "Would you like to repeat the program? Y/N" << endl;

    cin >> A;

  }while (A == 'Y');

 

  cout << "Thanks for using this program. Have a nice day "<< endl;

  return 0;

  

}