Monday, July 6, 2020

Write a program to input two integers and find the GCD/HCF of two numbers (using division method)? (10,12, HCF=2)

41)   Write a program to input two integers and find the GCD/HCF of two numbers (using division method)? (10,12,   HCF=2)

import java.util.*;
public class GCD
{
    public static void main(String args[ ])
   {
      Scanner sc = new Scanner (System.in);
      System.out.println("Enter two numbers");
      int a = sc.nextInt();
      int b = sc.nextInt();
      int k = Math.min(a,b);
      b = Math.max(a,b);
      a=k;
      while(k!=0)
      {
          k=b%k;
          if(k!= 0)
          {
              b=a;
              a=k;
          }
      }
      System.out.println("G.C.D = "+a);
   }
}




No comments:

Post a Comment

if you have any doubts, Please let me know.
Your Truly
Shashank Porwal

 jhvghvgk ;nkjn ,nkjhj