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.
How to find the GCD of two numbers?
RUN CODE SNIPPETSource 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; }
Input
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.
- 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 usingcout<<
 and the Insertion Operators'<<‘ , ‘>>’. - 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.
- if(n1>n2) n1 -= n2; else n2 -= n1;
- The Function
n1 -= n2
is the shortened version ofn1 = 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.
n1 | n2 | n1>n2 | n1 -= n2 | n2 -=n1 | n1 !=n2 |
4 | 10 | (4 > 10)false | – | 10 – 4 = 6 n = 6 | (4!=6)true |
4 | 6 | (4 > 6)false | – | 6 – 4 = 2n2 = 2 | (4!=2)true |
4 | 2 | (4 > 2)true | 4 – 2 = 2n1 =2 | – | (2!=2)false |
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.