Smallest number divisible by first n numbers | Java program to find the smallest number evenly divisible by all numbers 1 to n ; Function for gcd ; Function returns the lcm of first n numbers ; Driver program to test the above function
class GFG { static long gcd ( long a , long b ) { if ( a % b != 0 ) return gcd ( b , a % b ) ; else return b ; } static long lcm ( long n ) { long ans = 1 ; for ( long i = 1 ; i <= n ; i ++ ) ans = ( ans * i ) / ( gcd ( ans , i ) ) ; return ans ; } public static void main ( String [ ] args ) { long n = 20 ; System . out . println ( lcm ( n ) ) ; } }