# Finding Greatest Common Divisor (GCD) of two numbers in C++

In mathematics, the Greatest Common Divisor (GCD), also referred to as sometimes Greatest Common Factor (GCF), Highest Common Factor (HCF), Greatest Common Measure (GCM), Highest Common Divisor (HCD) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. This program will find Greatest Common Divisor (GCD) using C++. It will take two values from the user as input and and compute its GCD.

Following is the sequence of interaction with user when the program is run:

1. The User will be asked to “Enter the First Number: “.

2. The User will be asked to “Enter the Second Number: “.

3. Message will be Displayed “Computing GCD”.

4. Result will be displayed to the User “Greatest Common Divisor (GCD) is: “.

#include<iostream> using namespace std; int main() { int first_num; cout<<"Enter First Number: ";cin>>first_num; int second_num; cout<<"Enter Second Number: ";cin>>second_num; int gcd; cout<<"Computing GCD ... "<<endl; for(int i=1; i<=first_num && i<=second_num; i++) { if(first_num%i==0 && second_num%i == 0 ){ gcd=i; } } cout<<"Greatest Common Divison (GCD) is: "<<gcd<<endl; return 0; }

### Related Posts

- Fibonacci series program in C++
- This Pointer, Static Members and destructors in C++
- Finding Longest Common Subsequence in two String using C++
- Introduction to Classes and Objects in C++
- Comparison of Sorting Algorithms – Bubble Sort, Selection Sort, Insertion Sort
- Applications of Circular Linked List in Data Structures
- More on Destructors and Static Data Members in C++
- Implementing Dijkstra Algorithm using Priority Queue (Heap) in C++
- Reading Text Files in C/C++
- Zodiac Signs Program in C++ and Java
- Guess My Number Game using C++
- Pointers, Arrays and Structures in C++

### Popular Posts (last 30 days)

- Attendance Management Sys… 1185 view(s)
- Graph Implementation in C… 587 view(s)
- Implementing Stack Data S… 578 view(s)
- Circular Linked Lists 524 view(s)
- Applications of Stack in … 523 view(s)
- Simple Currency Converter… 442 view(s)
- GRASP Design Patterns 328 view(s)
- Advanced Data Structures … 265 view(s)
- C++ Tutorial for Intermed… 253 view(s)
- Sockets and Network Progr… 185 view(s)

### Related Links

### Tags

Android C-Sharp C/C++ language Classes Data structures Design Pattern Eclipse Game Development Graphics Design Books HTML iPhone JAVA JAVA GUI MIPS Assembly Mobile Programming Books Object Oriented PDF PHP Programming Programming Books Programming Languages Books Python RaphaelJS REST Source Code Threads Tutorial Web Development Books