Problem 3.12

 

Question 3.12:
Find GCD of two numbers using for loop and if statement.


Solution: 


package com.company;
import java.util.Scanner;
public class lab3problem12
{
    public static void main(String[] args) {
        Scanner Obj = new Scanner(System.in);
        System.out.println("Input first integer:");
        int n = Obj.nextInt();
        System.out.println("Input Second integer: ");
        int m = Obj.nextInt();
        int gcd = 1;

        for(int i = 1; i <= n && i <= m; ++i)
        {

            if(n % i==0 && m % i==0)
                gcd = i;
        }

        System.out.println("G.C.D of  and  "+ gcd);
    }
}

Previous Post Next Post