 # C++ Programming Code Examples

## C++ > Recursion Code Examples

### program to Find Sum of Natural Numbers using Recursion

``` program to Find Sum of Natural Numbers using Recursion Example to find the sum of natural numbers by using a recursive function. The positive numbers 1, 2, 3... are known as natural numbers. The program below takes a positive integer from the user and calculates the sum up to the given number. You can find the sum of natural numbers using loops as well. However, you will learn to solve this problem using recursion here In this program, the number entered by the user is passed to the add() function. Suppose, 10 is entered by the user. Now, 10 is passed to the add() function. This function adds 10 to the addition result of 9 (10 - 1 = 9). Next time, 9 is added to the addition result of 8 (9 - 1 = 8). This goes on until the number reaches 0, when the function returns 0. Now, every function is returned to calculate the end result: 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55. #include<iostream> using namespace std; int add(int n); int main() { int n; cout << "Enter a positive integer: "; cin >> n; cout << "Sum = " << add(n); return 0; } int add(int n) { if(n != 0) return n + add(n - 1); return 0; } ```

Find the Length of the String by Recursion - C Program uses recursive function and counts the number of nodes in a Linked List. A linked list is an 'Ordered Set' of data elements, each 'containing a link' to its successor. Recursive C

Find the Trace & Normal of a given Matrix - C Program find the trace & normal of a given matrix. Trace of the matrix is the sum of the elements of the main diagonal i.e diagonal from the upper left to the lower right of a...

Code fscanf Works Exactly Like the scanf() - C Programming fscanf: works exactly like the scanf() function except that it reads from the stream. Returns the number of arguments actually assigned values or EOF on failure &

Code Display the IP Address of the System - Create a socket to define network interface IPv4. Define the IPv4 address type. Define the port name where network is attached. Access the network interface information...

C++ Language Passing Arrays to Function - In this C++ Code, we are passing 2 arrays a & b to the "function sum()". Function adds the "corresponding elements" of both the arrays and display them. This c++ function adds the

2D-Drawing Circles, Arcs, Rectangle, Lines - This 2D-Drawing Program is used for drawing circles, arcs, rectangle, lines and many more. Controls for the two-dimensional drawing. Enter radius of circle, enter radius, starting...

Use Ternary Operator (?:) C Programming - The first expression conditionalExpression is evaluated first. This expression evaluates to 1 if it's true and evaluates to 0 if it's false. If the conditionalExpression is true, expression1 is

Program to Implement Sorted Linked List - Program demonstrates the implementation of Sorted linked list to Balanced BST. Counts the number of nodes in linked list. Construct balanced BST and returns root of it. Returns