 # C++ Programming Code Examples

## C++ > Recursion Code Examples

### C++ program to Calculate Factorial of a Number Using Recursion

``` C++ program to Calculate Factorial of a Number Using Recursion Example to find factorial of a non-negative integer (entered by the user) using recursion. This program takes a positive integer from user and calculates the factorial of that number. Suppose, user enters 6 then, Factorial will be equal to 1*2*3*4*5*6 = 720 You'll learn to find the factorial of a number using a recursive function in this example. In the above program, suppose the user inputs a number 6. The number is passed to the factorial() function. In this function, 6 is multiplied to the factorial of (6 - 1 = 5). For this, the number 5 is passed again to the factorial() function. Likewise in the next iteration, 5 is multiplied to the factorial of (5 - 1 = 4). And, 4 is passed to the factorial() function. This continues until the value reaches 1 and the function returns 1. Now, each function returns the value back to compute 1 * 2 * 3 * 4 * 5 * 6 = 720, which is returned to the main() function. #include<iostream> using namespace std; int factorial(int n); int main() { int n; cout << "Enter a positive integer: "; cin >> n; cout << "Factorial of " << n << " = " << factorial(n); return 0; } int factorial(int n) { if(n > 1) return n * factorial(n - 1); else return 1; } ```

C++ Code Deletes Elements from an Array - To "delete element" from an array in C++, you have to first ask to the user to enter the array size then ask to enter the array element, ask to enter the element which is "to be deleted".

Reference Operator Dereference Operator - The pointer is a C++ programming data type whose value refers directly to (or "points to") another value stored elsewhere in computer memory using its address. Pointer examples

C Nth Fibonacci Numbers using Recursion - Prints the Fibonacci of a given number using recursion. In fibonacci series, each number is the sum of the two preceding numbers. 0, 1, 1, 2, 3, 5, 8, ... The following program returns

Qsort Sort [array of pointers to structures] - Code compare function for qsort. Add ONE element to the array. Allocate memory for ONE 'struct node'. Copy the data into the new element ( structure ). Qsort array of...

Convert Decimal to Binary number system - C program to input any decimal number from user and convert it to binary number system using bitwise operator. How to convert from decimal number system to binary number...

C++ SQL Connects to ODBC Data Sources - Allocating memory for "ODBC" Environment handle. Connecting to the data source "db97" using userid and password. Prepare the SQL statement by assigning it to the "statement"

Find the volume & surface area of cuboids - This C Program to calculates the volume and surface area of cuboids. The Formula used in this program are surfacerea= 2(w * l + l * h + h * w) where w is Width & l is Length and h is

C++ Language Do While Loop Statements - The statement is executed, then expression is evaluated. If it is true, statement is evaluated again, and so on. When 'expression' becomes false, the 'loop terminates'. Experience shows