FlowChart Examples Pseudocode Examples

Pseudocode to Find GCD of Two Numbers4 min read

In this program, we’ll learn to find Greatest Common Divisor (GCD) of two numbers in Algortihm.

Pseudocode:




This is a pseudocode for finding the greatest common divisor (GCD) of two numbers n1 and n2. The GCD is the largest positive integer that divides both n1 and n2 without a remainder.

Here is a detailed explanation of the code:

  • The code declares three variables: n1, n2, and gcd. n1 and n2 will be used to store the two input numbers, and gcd will be used to store the greatest common divisor.
  • The code prompts the user to enter the first number and stores it in n1.
  • The code prompts the user to enter the second number and stores it in n2.
  • The code starts a loop that iterates from 1 to the minimum of n1 and n2 (FOR i = 1; i <= n1 && i <= n2; ++i THEN).
  • Inside the loop, the code checks if i divides both n1 and n2 without a remainder (IF n1 % i == 0 && n2 % i == 0 THEN). If this condition is true, it updates the value of gcd to i.
  • After the loop ends, the code prints the GCD of n1 and n2 to the console.

This pseudocode uses a simple and efficient method for finding the GCD of two numbers, known as the “iterative” method. It starts from the smallest number and iteratively checks if each number from 1 up to the smallest number divides both numbers without a remainder. The first number that does is the GCD.

Flowchart:

Java Code: Java Program to find gcd of two numbers using for loop

Python Code: Write a program to Find GCD of Two Numbers in Python

JavaScript Code: Write a program to Find GCD of Two Numbers in JavaScript

Output:

Leave a Comment