HomeC++C++ Program to Find GCD of two Numbers

# C++ Program to Find GCD of two Numbers

In this post, you will learn how to Find GCD of two Numbers using C++ programming language.

This lesson will teach you how to find the GCD of two numbers, with a while loop, decision making statements and a mathematical function using the C++ Language. Letâ€™s look at the below source code.

## Source Code

```#include <iostream>
using namespace std;
int main()
{
int n1, n2;
cin >> n1 >> n2;
cout << "Enter two numbers: "<< n1 <<","<< n2 << endl;
while(n1 != n2)
{
if(n1 > n2)
n1 -= n2;
else
n2 -= n1;
}
cout << "\nHCF = " << n1;
return 0;
}```

```10
4```

## Output

```Enter two numbers: 10,4
HCF = 2```

#include <iostream>

• This line which is called the header file.Â `#include` statement tells the compiler to use available files and `<iostream>`is the name of the specific that we have used in this code. The `<iostream>` file stands for Input and Output statement.

using namespace std;

• The C++ has a standard library that has files for different functions and this line is used to access the standard file for input and output statements.

int main();

• This line usually controls the function of the code, as it calls the functions to perform their tasks.
• The `int main()`shows that the input value is a type of integer, once the program is executed the function returns to the main function, by using the statement â€˜return 0;â€™.

{ }

• The opening â€˜ { â€˜ and the closing â€˜ } â€˜ curly braces mark the start and the finish of the main function
• Every statement and value between these braces belong to the main function.

The above statements are the main factors that support the function of the source code. Now we can look into the working and layout of the codeâ€™s function.

1. Declare the the variablesÂ n1, n2 asÂ integers. And collect their valuesÂ from the user and store it in the variable n1 and n2, using function `cin>>`Â  and display the value using `cout<<`Â and the Insertion Operators'<<â€˜ , â€˜>>â€™.
2. In the above code the smaller number is subtracted from the larger number, this condition is expressed by the following if else statement and according to that the mathematical functions are executed.
3. if(n1>n2) n1 -= n2; else n2 -= n1;
4. The Function `n1 -= n2` is the shortened version of `n1 = n1 - n2` .After the calculation the number is stored in n1. The following table will explain how theÂ while loop and the if else statement functions work.

To explain the above table, first the if statement is verified, when it is not true the else statement is executed. Then the execution moves back up to the while loop. TheÂ while loop is executed continuously until the loop turns out to be false, then the execution is terminated and the output is displayed. And hence the answer is 2.

Note: The â€˜ << endl â€˜ in the code is used to end the current line and move to the next line and â€˜\nâ€™ is also a new line function, to understand how both the functions work exclude it from the code, move it around and work with it.

### You May Also Like

#### C++ Program to Generate Multiplication Table

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Display Fibonacci Sequence

In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
• C++
• January 30, 2022

#### C++ Program to Find LCM of two Numbers

In this post, you will learn how to Find LCM of two Numbers using C++ programming language. This lesson will...
• C++
• January 30, 2022