LCM of First n Natural Numbers | Java program to find LCM of First N Natural Numbers ; to calculate hcf ; to calculate lcm ; lcm ( a , b ) = ( a * b ) / hcf ( a , b ) ; assign a = lcm of n , n - 1 ; b = b - 1 ; Driver code . ; base case ; Function call pass n , n - 1 in function to find LCM of first n natural number
public class Main { static int hcf ( int a , int b ) { if ( b == 0 ) return a ; return hcf ( b , a % b ) ; } static int findlcm ( int a , int b ) { if ( b == 1 ) return a ; a = ( a * b ) / hcf ( a , b ) ; b -= 1 ; return findlcm ( a , b ) ; } public static void main ( String [ ] args ) { int n = 7 ; if ( n < 3 ) System . out . print ( n ) ; else System . out . print ( findlcm ( n , n - 1 ) ) ; } }