Object
Write a program to find and print the Greatest Common Divisor (G.C.D.) of two numbers typed in.
Algorithm
- Declare integer variables a, b and c.
- Read a and b.
- c = 1.
- Repeat WHILE b ≠ 0
- c = a mod b.
- a = b.
- b = c.
- [End of WHILE loop.]
- Write a.
- Exit.
Flowchart

C++ Source Code
// program 56
#include<iostream>
using namespace std;
int main()
{
int a, b, c;
cout << "Enter the first number: ";
cin >> a;
cout << "Enter the second number: ";
cin >> b;
c = 1;
while (b != 0)
{
c = a % b;
a = b;
b = c;
}
cout << "The G.C.D. of the two numbers is: " << a;
return 0;
}
C Source Code
/*program 56*/
#include<stdio.h>
int main()
{
int a, b, c;
printf("Enter the first number: ");
scanf("%d", &a);
printf("Enter the second number: ");
scanf("%d", &b);
c = 1;
while(b!=0)
{
c = a%b;
a = b;
b = c;
}
printf("The G.C.D. of the two numbers is: %d", a);
return 0;
}
Output
Enter the first number: 12
Enter the second number: 20
The G.C.D. of the two numbers is: 4
