 # C++ Programming Code Examples

## C++ > Recursion Code Examples

### Find GCD of Two Numbers Using Recursive Euclid Algorithm

``` Find GCD of Two Numbers Using Recursive Euclid Algorithm This is a C++ Program to find GCD of two numbers using Recursive Euclid Algorithm. In mathematics, the Euclidean algorithm, or Euclid's algorithm, is a method for computing the greatest common divisor (GCD) of two (usually positive) integers, also known as the greatest common factor (GCF) or highest common factor (HCF). It is named after the Greek mathematician Euclid, who described it in Books VII and X of his Elements. The GCD of two positive integers is the largest integer that divides both of them without leaving a remainder (the GCD of two integers in general is defined in a more subtle way). In its simplest form, Euclid's algorithm starts with a pair of positive integers, and forms a new pair that consists of the smaller number and the difference between the larger and smaller numbers. The process repeats until the numbers in the pair are equal. That number then is the greatest common divisor of the original pair of integers. The main principle is that the GCD does not change if the smaller number is subtracted from the larger number. For example, the GCD of 252 and 105 is exactly the GCD of 147 (= 252 - 105) and 105. Since the larger of the two numbers is reduced, repeating this process gives successively smaller numbers, so this repetition will necessarily stop sooner or later - when the numbers are equal (if the process is attempted once more, one of the numbers will become 0). #include<iostream> #include<conio.h> #include<stdlib.h> using namespace std; int gcd(int u, int v) { return (v != 0) ? gcd(v, u % v) : u; } int main(void) { int number1, number2, result; cout << "Enter two numbers to find GCD using Euclidean algorithm: "; cin >> number1 >> number2; result = gcd(number1, number2); if (gcd) cout << "\nThe GCD of " << number1 << " and " << number2 << " is: " << result << endl; else cout << "\nInvalid input!!!\n"; return 0; } ```

C Program Check the String is Palindrome - C program code to 'Check whether a String' is palindrome or not without using loop. How to Check whether a string is 'Palindromic String' or not in C. Palindrome string is special string

C Programming Code N-Queen's Problem - Enter the number of queens(say n) and watch how computer places them in (n x n) matrix such that none can meet another moving along horizontally, vertically or digonally.

C Finds All Roots of A Quadratic Equation - Step by step descriptive logic to find roots of Quadratic Equation using Switch Case. Input coefficients of quadratic equation. Store it in some variable say a, b, c. Find discriminant of

C++ Coding Sorts a Data using Heap Sort - Heap sort is comparison based algorithm. It's selection sort sample. The time complexity is O(n*log(n)). Build a max heap using the given data element. And then Delete the root node

C++ Check a Number is Palindrome or not - Palindrome number is a number that remains the same when its digits are reversed. 16461, for example: we take 121 and reverse it, after revers it is same as original. Get a value from

Converts Hexadecimal to Binary Numbers - Hexadecimal to Binary conversion is divided in 3 steps. Extract each hex digits separately. Find the Binary of each extracted Hex digit. Store the binary equivalent of extracted hex

C Program to print Even numbers without - The above approach to list Even Numbers is not optimal. It unnecessarily iterates for odd numbers which is a performance issue and to overcome this start the loop with first even...

Learn About Structure of a C++ Program - A C++ program starts its execution from this method "main". Wherever this main method is, the main method will be executed first. '()' is used at the end as main is a method. Main