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.

How to find the GCD of two numbers?

RUN CODE SNIPPET

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;
}

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.

  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.
n1n2n1>n2n1 -= n2n2 -=n1n1 !=n2
410(4 > 10)false10 – 4 = 6 n = 6(4!=6)true
46(4 > 6)false6 – 4 = 2n2 = 2(4!=2)true
42(4 > 2)true4 – 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.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

In this post, you will learn how to Generate Multiplication Table using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Display Fibonacci Sequence using C++ programming language. This lesson will teach you...
  • C++
  • January 30, 2022
In this post, you will learn how to Find LCM of two Numbers using C++ programming language. This lesson will...
  • C++
  • January 30, 2022