{"id":1347,"date":"2022-03-03T07:13:37","date_gmt":"2022-03-03T07:13:37","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1347"},"modified":"2022-03-03T07:13:37","modified_gmt":"2022-03-03T07:13:37","slug":"find-lcm-of-two-numbers","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/03\/03\/find-lcm-of-two-numbers\/","title":{"rendered":"Find LCM of two Numbers"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">LCM using while Loop and if Statement<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>public class Main {\n  public static void main(String&#91;] args) {\n\n    int n1 = 72, n2 = 120, lcm;\n\n    \/\/ maximum number between n1 and n2 is stored in lcm\n    lcm = (n1 &gt; n2) ? n1 : n2;\n\n    \/\/ Always true\n    while(true) {\n      if( lcm % n1 == 0 &amp;&amp; lcm % n2 == 0 ) {\n        System.out.printf(\"The LCM of %d and %d is %d.\", n1, n2, lcm);\n        break;\n      }\n      ++lcm;\n    }\n&nbsp; }\n}\n<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>The LCM of 72 and 120 is 360.<\/samp><\/code><\/pre>\n\n\n\n<p>In this program, the two numbers whose LCM is to be found are stored in variables&nbsp;<var>n1<\/var>&nbsp;and&nbsp;<var>n2<\/var>&nbsp;respectively.<\/p>\n\n\n\n<p>Then, we initially set&nbsp;<var>lcm<\/var>&nbsp;to the largest of the two numbers. This is because, LCM cannot be less than the largest number.<\/p>\n\n\n\n<p>Inside the infinite while loop (<code>while(true)<\/code>), we check if&nbsp;<var>lcm<\/var>&nbsp;perfectly divides both&nbsp;<var>n1<\/var>&nbsp;and&nbsp;<var>n2<\/var>&nbsp;or not.<\/p>\n\n\n\n<p>If it does, we&#8217;ve found the LCM. We print the LCM and break out from the while loop using&nbsp;<code>break<\/code>&nbsp;statement.<\/p>\n\n\n\n<p>Else, we increment&nbsp;<var>lcm<\/var>&nbsp;by 1 and re-test the divisibility condition.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>LCM using while Loop and if Statement Output In this program, the two numbers whose LCM is to be found are stored in variables&nbsp;n1&nbsp;and&nbsp;n2&nbsp;respectively. Then, we initially set&nbsp;lcm&nbsp;to the largest of the two numbers. This is because, LCM cannot be less than the largest number. Inside the infinite while loop (while(true)), we check if&nbsp;lcm&nbsp;perfectly divides [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[350],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1347"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1347"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1347\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}