
Introduction
This program helps to find Greatest Common Division from two given number. First of all we have to understand GCD, it is called as Greatest or highest common different in mathematics.
In Simple World we can say – the greatest common divisor (gcd) of two or more integers, which are not all zero, is the largest positive integer that divides each of the integers.
We can find it using multiplication formula, please see the example below –
Example 1:
Number 1: 10
Number 2: 15
So we have to find multiplication of first as well as for second number.
Number 1 = 5 X 2
Number 2 = 5 X3
————————–
Common Numbers are : 5
So 5 is GCD for 10 & 15.
Example 2:
Number 1: 70
Number 2: 60
So we have to find multiplication of first as well as for second number.
Number 1 = 2 X 5 X 7
Number 2 = 2 X 2 X 3 X 5
————————————
Common Numbers are : 2 X 5 = 10
10 is GCD for 70 & 60.
Flowchart
Please find sample flowchart diagram for this program as given below –
Program(Code) Using Java Language
package com.kw.sample;
import java.util.Scanner;
/**
* This class helps to find GCD from two given number. Like GCD for 10 and 20 is
* 10.
*
* @author dsahu1
*
*/
public class KWGCDExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the First no : ");
int n1 = sc.nextInt();
System.out.print("Enter the Second no : ");
int n2 = sc.nextInt();
while (n1 != n2) {
if (n1 > n2) {
n1 = n1 - n2;
} else {
n2 = n2 - n1;
}
}
System.out.println("Greatest Common Division is :" + n1);
}
}
Output
Scenario 1:
Enter the First no : 10
Enter the Second no : 20
Greatest Common Division is :10
Scenario 2:
Enter the First no : 1524
Enter the Second no : 258
Greatest Common Division is :6
Video Explanation
This video helps to understand flowchart and program which written in the above steps. There are various ways to write a program and we are not covering all of them as this is just for practice. Please post your program in the comment box so other can utilize it.