- Home
- Core Java Examples
- Gcm And Lcm Of Two Numbers
Core Java - Gcm And Lcm Of Two Numbers
विवरण :
कोई विवरण नहीं है |
सोर्स कोड :
import java.util.Scanner; public class LCMGCD { public static void main(String args[]) { int num1,num2,temp1,temp2,gcd=0,lcm; System.out.print("Enter two numeric values : "); Scanner enter = new Scanner(System.in); num1 = enter.nextInt(); num2 = enter.nextInt(); temp1=num1; temp2=num2; if(temp1 == 0) { gcd=temp2; } else if(temp2 == 0) { gcd=temp1; } else { while(temp2 != 0) { if(temp1 > temp2) { temp1=temp1-temp2; } else { temp2=temp2-temp1; } gcd=temp1; } } lcm = (num1*num2) / gcd ; System.out.println("LCM of " + num1 + " and " + num2 + " is " + lcm); System.out.println("GCD of " + num1 + " and " + num2 + " is " + gcd); } }
आउटपुट :
Enter two numeric values : 50 45 LCM of 50 and 45 is 450 GCD of 50 and 45 is 5