Java Online Compiler
Example: Java Program to Find GCD of Two Numbers using For loop
C
C++
C#
Java
Python
PHP
Main.java
STDIN
Run
// Java Program to Find GCD of Two Numbers using For loop import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int p, q, i, g = 0; // p & q - To store the two positive numbers // g - To store the GCD number System.out.println ("-----Enter the two positive integer numbers-----"); p = in.nextInt(); q = in.nextInt(); for (i = 1; i <= p && i <= q; ++i) { if (p % i == 0 && q % i == 0) { g = i; } } System.out.println ("\nThe GCD number if " + p + " & " + q + " is: " + g); } }
180 90
Output
Clear
ADVERTISEMENTS