LCM of First n Natural Numbers | C ++ 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int hcf ( int a , int b ) { if ( b == 0 ) return a ; return hcf ( b , a % b ) ; } int findlcm ( int a , int b ) { if ( b == 1 ) return a ; a = ( a * b ) / hcf ( a , b ) ; b -= 1 ; return findlcm ( a , b ) ; } int main ( ) { int n = 7 ; if ( n < 3 ) cout << n ; else cout << findlcm ( n , n - 1 ) ; return 0 ; }