Object

Write a program to find and print the Greatest Common Divisor (G.C.D.) of two numbers typed in.

Algorithm

  1. Declare integer variables a, b and c.
  2. Read a and b.
  3. c = 1.
  4. Repeat WHILE b ≠ 0
    • c = a mod b.
    • a = b.
    • b = c.
    • [End of WHILE loop.]
  5. Write a.
  6. 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

Design a site like this with WordPress.com
Get started