Program in C Plus Plus and C with explanation to find the Greatest Common Division (GCD) with flowchart
In this tutorial, we will learn about the followings;
- Flowchart of the program to find the Greatest Common Division (GCD)
- Program in C Plus Plus with an explanation to find the Greatest Common Division (GCD)
- Program in C with an explanation to find the Greatest Common Division (GCD)
Flowchart of the program to find the Greatest Common Division (GCD)

Program in C Plus Plus with an explanation to find the Greatest Common Division (GCD)
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int h,i;
cout<<"Program: Find GCD Of Given Numbers";
cout<<"\n Welcome to T4TUTorials! Please Enter 1st Number=";
cin>>h;
cout<<" Welcome to T4TUTorials! Please Enter 2nd Number=";
cin>>i;
while(h!=i)
{
if(h>i)
h=h-i;
else
i=i-h;
}
cout<<"The Greatest Common Division (GCD): "<<h;
}
Output


Program in C with an explanation to find the Greatest Common Division (GCD)
#include<stdio.h>
int main()
{
int h,i;
printf("Welcome to T4TUTorials! Please Enter 1st Number=");
scanf("%d", &h);
printf("\n Welcome to T4TUTorials! Please Enter 2nd Number=");
scanf("%d", &i);
while(h!=i)
{
if(h>i)
h=h-i;
else
i=i-h;
}
printf("\n The G.C.D Of Given Number is =:%d",h);
}
Output

Figure: Program in C Plus Plus and C with an explanation to find the Greatest Common Division (GCD) with the flowchart.
Greatest Common Factor by using Arrays in C++
Let’s see the program of “Greatest Common Factor by using Arrays in C++”.
#include<iostream>
using namespace std;
int main()
{
int a;
int gcm[100];
cout<<"Type two numbers"<<endl;
for(int i=0; i<2; i++)
{
cin>>gcm[i];
}
for(int i=1; i<=gcm[0]; i++)
{
if(gcm[0]%i==0&&gcm[1]%i==0)
{
a=i;
}
}
cout<<"Greatest Common Multiple is = "<<a<<endl;
}
Output
Type two numbers
3
2
Greatest Common Multiple is = 1
Greatest Common Factor by using a do-while loop in C++
Let’s see the program of “Greatest Common Factor by using a do-while loop in C++”.
#include <iostream>
using namespace std;
int main()
{
int x, y;
cout<<"Please Enter first number :"<<endl;
cin >> x;
cout<<"Please Enter second number :"<<endl;
cin >> y;
{
do
{
if(x > y)
x -= y;
else
y -= x;
}
while(x !=y);
}
cout << "GCD is = " << x;
return 0;
}
Greatest Common Factor by using a for loop in C++
Let’s see the program of “Greatest Common Factor by using a for loop in C++”.
#include <iostream>
using namespace std;
int main()
{
int a, b, x;
cout << "Enter First number: "<<endl;
cin>>a;
cout <<"Enter second number: "<<endl;
cin>>b;
for (int i = 1; i <= a; i++)
{
if (a % i == 0 && b % i ==0)
x = i;
}
cout << "GCD is = " << x;
return 0;
}
Greatest Common Factor by using a While loop in C++
Let’s see the program of “Greatest Common Factor by using While loop in C++”.
#include <iostream>
using namespace std;
int main()
{
int x, y;
cout<<"Enter first number :"<<endl;
cin >> x;
cout<<"Enter second number :"<<endl;
cin >> y;
while(x != y)
{
if(x > y)
x -= y;
else
y -= x;
}
cout << "GCD is = " << x;
return 0;
}
Greatest Common Factor by using If Else Statement in C++
Let’s see the program of “Greatest Common Factor by using If Else Statement in C++”.
#include<iostream>
using namespace std;
int gcm(int,int,int&);
int main()
{
int a, b, c;
cout<<"enter two numbers"<<endl;
cin>>a>>b;
if (a<0 || b<0)
{ cout<<"negative values not allowed"<<endl;
}
else
{
gcm(a,b,c);
cout<<"GCM is = "<<c<<endl;
}
}
int gcm(int j, int k, int &l)
{
for(int i=1; i<=j; i++)
{
if(j%i==0 && k%i==0)
{
l=i;
}
}
return l;
}
Greatest Common Factor by using Switch Statement in C++
Let’s see the program of “Greatest Common Factor by using Switch Statement in C++”.
#include <iostream>
using namespace std;
int main()
{
int a, b, x;
char opr;
cout << "Enter First number: "<<endl;
cin>>a;
cout <<"Enter second number: "<<endl;
cin>>b;
cout<<"You still want to calculate?";
cin>>opr;
switch(opr)
{
case 'y':
for (int i = 1; i <= a; i++)
{
if (a % i == 0 && b % i ==0)
x = i;
}
cout << "GCD is = " << x;
break;
case 'n':
cout<<"We cant show you result.";
break;
default:
cout<<"invalid";
}
return 0;
}
Greatest Common Factor by using the User Define Function in C++
Let’s see the program of “Greatest Common Factor by using the User Define Function in C++”.
#include<iostream>
using namespace std;
int gcm(int,int,int&);
int main()
{
int a, b, c;
cout<<"enter two numbers"<<endl;
cin>>a>>b;
gcm(a,b,c);
cout<<"GCM is = "<<c<<endl;
}
int gcm(int j, int k, int &l)
{
for(int i=1; i<=j; i++)
{
if(j%i==0 && k%i==0)
{
l=i;
}
}
return l;
}
Greatest Common Factor by using the Nested If Statement in C++
Let’s see the program of “Greatest Common Factor by using the Nested If Statement in C++”.
#include<iostream>
using namespace std;
int gcm(int,int,int&);
int main()
{
int a, b, c;
cout<<"enter two numbers"<<endl;
cin>>a>>b;
if (a<0 || b<0)
{ cout<<"negative values not allowed"<<endl;
}
else
{
if(a==0 || b==0)
{
cout<<"plz type numbers greater than Zero"<<endl;
}
else
{
gcm(a,b,c);
cout<<"GCM is = "<<c<<endl;
}
}
}
int gcm(int j, int k, int &l)
{
for(int i=1; i<=j; i++)
{
if(j%i==0 && k%i==0)
{
l=i;
}
}
return l;
}