Open In App

Structure of C++ Program

The C++ program is written using a specific template structure. The structure of the program written in C++ language is as follows:



Documentation Section:




/*     This is a C++ program to find the
        factorial of a number
 
    The basic requirement for writing this
    program is to have knowledge of loops
 
    To find the factorial of number
    iterate over range from number to one
*/

Linking Section:

The linking section contains two parts:

Header Files:



#include<iostream>

Namespaces:

using namespace std;

Definition Section:

Global Declaration Section:

Function Declaration Section:




// Function to implement the
// factorial of number num
INTEGER factorial(INTEGER num)
{
    // Iterate over the loop from
    // num to one
    for (INTEGER i = 1; i <= num; i++) {
        fact *= i;
    }
 
    // Return the factorial calculated
    return fact;
}

Main Function:

Below is the program to illustrate this:




// Documentation Section
/*  This is a C++ program to find the
    factorial of a number
    The basic requirement for writing this
    program is to have knowledge of loops
    To find the factorial of a number
    iterate over the range from number to 1
*/
 
// Linking Section
#include <iostream>
using namespace std;
 
// Definition Section
#define msg "FACTORIAL\n"
typedef int INTEGER;
 
// Global Declaration Section
INTEGER num = 0, fact = 1, storeFactorial = 0;
 
// Function Section
INTEGER factorial(INTEGER num)
{
    // Iterate over the loop from
    // num to one
    for (INTEGER i = 1; i <= num; i++) {
        fact *= i;
    }
 
    // Return the factorial
    return fact;
}
 
// Main Function
INTEGER main()
{
    // Given number Num
    INTEGER Num = 5;
 
    // Function Call
    storeFactorial = factorial(Num);
    cout << msg;
 
    // Print the factorial
    cout << Num << "! = "
         << storeFactorial << endl;
 
    return 0;
}

Output
FACTORIAL
5! = 120

Time Complexity: O(N)
Auxiliary Space: O(1)


Article Tags :