 # C++ Programming Code Examples

## C++ > Mathematics Code Examples

### Find Greatest Common Divisor (GCD) of two numbers c++ program

``` Find Greatest Common Divisor (GCD) of two numbers c++ program What is GCD of two numbers? It means a greatest number which divides both numbers For example: Two numbers are 18 and 24 Numbers which divides both are 1, 2, 3 and 6 in which greatest number is 6 So 6 is the GCD of 18 and 24 Program takes input two numbers A for loop which terminates if loop variable which is 'i' is greater than one of two numbers and operator is used to maintain this condition Within for loop an if condition is used to check if variable 'i'divides both of them it will be saved in GCD variable as value of 'i' is changing during each iteration in increasing order if another number divides both of them it will replace the previous value of GCD After the loop termination our program will show the GCD of two number as the result #include<iostream> using namespace std; int main() { int first_number; cout<<"Enter First Number : ";cin>>first_number; int second_number; cout<<"Enter Second Number: ";cin>>second_number; int gcd; for(int i=1;i<=first_number&&i<=second_number;i++){ if(first_number%i==0 && second_number%i == 0 ){ gcd=i; } } cout<<"Greatest Common Divison (GCD):"<<gcd<<endl; return 0; } ``` 