Search

LCM of two numbers in C || C Programs

Code -

#include <stdio.h> 
#include <conio.h> 
void main()  
{  
 int num1, num2, max_div, flag = 1;  
 // accept any two positive number from the user 
    printf( " Enter any two positive numbers to get the LCM \n ");  
    scanf(" %d %d", &num1, &num2);  
 
 // max_div variable holds the max divisible number between num1 and num2. 
    max_div = (num1 > num2) ? num1 : num2;  
 
 while (flag) // (flag = 1) 
    {  
 if (max_div % num1 == 0 && max_div % num2 == 0)  
        {  
            printf( " The LCM of %d and %d is %d. ", num1, num2, max_div);  
 break;  
        }  
        ++max_div; // pre-increment max_div 
    }  
}  

Output -

Enter any two positive numbers to get the LCM
 15
12
 The LCM of 15 and 12 is 60.

4 views0 comments

Related Posts

See All

Program of Random Number in C || C Programs

Code - #include <stdio.h> #include <conio.h> #include <stdlib.h> void main() { // use rand() function to generate the number printf (" The random number is: %d", rand()); pri

Getchar() function in C || C Programs

Code - #include <stdio.h> #include <conio.h> void main() { char c; printf ("\n Enter a character \n"); c = getchar(); // get a single character printf(" You have passed ")

  • 1024px-Telegram_logo.svg
  • download
  • YouTube