Java inputs two positive integers m and N, seeking their greatest common divisor and least common multiple.

Source: Internet
Author: User
Tags gcd greatest common divisor

import java.util.scanner;public class test6 {    //Euclidean   Euclidean method &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;GCD (int a,int b)  {         int r ;        while (b !=  0)  {            r = a %  b ;            a = b;             b = r;         }        return a;    Another solution to  }    //least common multiple     public static int gcdtwo ( INT&NBSP;A,INT&NBSP;B) {        while  (a != b)  {             if  (a > b )  {                 a = a -b;             } else {                 b = b - a;             }        }         return a;    }    // Greatest common divisor &NBSP;&NBSP;&NBSP;&NBSP;PUBLIC&NBSP;STATIC&NBSP;INT&NBSP;LCM (int a,int b)  {   &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RETURN&NBSP;A&NBSP;*&NBSP;B&NBSP;/GCD (A, b);    }     public static void main (String[] args)  {         sCanner input = new scanner (system.in);         System.out.println ("Please enter first number:");        int a =  Input.nextint ();         system.out.println ("Please enter a second number:");         int b = input.nextint ();         /*if  (a < b)  {             a = a+b;            b  = a - b;            a =  a - b;        }*/                 system.out.println (Gcdtwo (b));         systEm.out.println (LCM (b));             }} 


This article is from the "Orange Growth Record" blog, be sure to keep this source http://azhome.blog.51cto.com/9306775/1546770

Java inputs two positive integers m and N, seeking their greatest common divisor and least common multiple.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.