Java Tutorial
In java programming, this programs are used to compute the HCF and LCM values for given user input numbers using for loop and modulo(%) operator.
This program is used to calculate the HCF value for numbers provided by user input.
import java.io.*; class HCFTest { public static void main(String args[])throws IOException{ int hcf =1,number1, number2; DataInputStream in= new DataInputStream(System.in); System.out.println("HCF Computation:"); System.out.println("-------------------"); System.out.print("Enter Number1: "); number1 = Integer.parseInt(in.readLine()); System.out.print("Enter Number2: "); number2 = Integer.parseInt(in.readLine()); for(int i=1; i<=(number1+number2)/2;i++){ if(number1%i==0 && number2%i==0) hcf = i; } System.out.println("HCF of "+number1+" and "+number2+": "+hcf); } }Output:
D:\Java_Programs>javac HCFTest.java D:\Java_Programs>java HCFTest HCF Computation: ------------------- Enter Number1: 30 Enter Number2: 25 HCF of 30 and 25: 5
This program is used to calculate the LCM value for numbers provided by user input.
import java.io.*; class LCMTest { public static void main(String args[])throws IOException{ int hcf =1, lcm=1, number1, number2; DataInputStream in= new DataInputStream(System.in); System.out.println("LCM Computation:"); System.out.println("-------------------"); System.out.print("Enter Number1: "); number1 = Integer.parseInt(in.readLine()); System.out.print("Enter Number2: "); number2 = Integer.parseInt(in.readLine()); for(int i=1; i<=(number1+number2)/2;i++){ if(number1%i==0 && number2%i==0) hcf = i; } lcm = (number1*number2) / hcf; System.out.println("LCM of "+number1+" and "+number2+": "+lcm); } }Output:
D:\Java_Programs>javac LCMTest.java D:\Java_Programs>java LCMTest LCM Computation: ------------------- Enter Number1: 30 Enter Number2: 25 LCM of 30 and 25: 150
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us | Report website issues in Github | Facebook page | Google+ page