Flowchart to find lcm of two numbers in c

In this tutorial, we will learn about the followings;

  1. Flowchart of a program of LCM of a number
  2. Program of LCM of a number in C Plus Plus (C++)
  3. Program of LCM of a number in C 

Flowchart of a program of LCM of a number

Flowchart to find lcm of two numbers in c
Figure: Program of LCM of a number in C Plus Plus C++ and C with the flowchart.

Program of LCM of a number in C Plus Plus (C++)

#include<iostream> #include<conio.h> using namespace std; void lcm(int,int); int main() { int p,q; cout<<"Enter two numbers: "; cin>>p; cin>>q; lcm(p,q); getch(); } void lcm(int p,int q) { int i,j; i=p; j=q; while(i!=j) { if(i < j) { i=i+p; } else { j=j+q; } } cout<<"\nL.C.M of two number p="<<p<<" and q="<<q<<" is "<<i; }

    cout<<"Enter two numbers: ";

    cout<<"\nL.C.M of  two number p="<<p<<" and q="<<q<<" is "<<i;

Output

Flowchart to find lcm of two numbers in c
Figure: Program of LCM of a number in C Plus Plus and C with the flowchart.

Program of LCM of a number in C 

#include<stdio.h> #include<conio.h> void lcm(int,int); int main() { int p,q; printf("Enter two numbers: "); scanf("%d %d",&p,&q); lcm(p,q); getch(); } void lcm(int p,int q) { int i,j; i=p; j=q; while(i!=j) { if(i < j) { i=i+p; } else { j=j+q; } } printf("\nL.C.M of p= %d and q= %d is: %d",p,q,i); }

    printf("Enter two numbers: ");

    printf("\nL.C.M of p= %d and q= %d is: %d",p,q,i);

Output

Flowchart to find lcm of two numbers in c
Figure: Program of LCM of a number in C Plus Plus and C with the flowchart.

In this topic, we will discuss the LCM and how we can get the LCM of two numbers in the C programming language.

Flowchart to find lcm of two numbers in c

LCM is a mathematical term that stands for Least Common Multiple (LCM). It is the smallest positive number that is completely divisible by both integer's n1 and n2, without leaving any remainder. LCM is also known as the Lowest Common Multiple. It is represented as LCM (a, b) or lcm (a, b). For example, the LCM of two positive numbers, 72 and 120, is 360.

Algorithm of LCM

Following are the algorithm of LCM of two number, as follows:

Step 1: Initialize the positive integer variables A and B.

Step 2: Store the common multiple of A & B into the max variable.

Step 3: Validate whether the max is divisible by both variables A and B.

Step 4: If max is divisible, display max as the LCM of two numbers.

Step 5: Else, the value of max is increased, and go to step 3.

Step 6: Stop the program.

LCM of two numbers using while loop

Let's consider an example to find the LCM of two numbers in C using while loop.

Lcm.c

#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 } } </conio.h></stdio.h>

Output

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

As we can see in the above program, we have passed two positive numbers, 15 and 12, stored in variable num1 and num2. Where the max_div variable store the largest divisible number by both variables num1 and num2. However, the LCM of two numbers cannot be less than max_div.

In each iteration of the while loop, max_div checked the number divisible by num1 and num2 variables inside the if condition.

if (max_div % num1 == 0 && max_div % num2 == 0) { }

If the above condition is not true, the max_div is incremented by 1, and the iteration of the loop continues till the if statement is true.

LCM of two numbers using GCD

Let's consider a program to get the LCM of two numbers in C using the GCD.

Gcd.c

#include <stdio.h> #include <conio.h> int main() { // declaration of the local variables int num1, num2, i, gcd, LCM; printf (" Enter any two positive numbers: \n "); scanf (" %d %d", &num1, &num2); /* use for loop to define the num1 & num2. Where the num1 and num2 should be equal or less than i. */ for ( i = 1; i <= id></=></conio.h></stdio.h>

Output

Enter any two positive numbers: 30 20 The LCM of two numbers 30 and 20 is 60.

LCM of two numbers using function

Let's consider a program to get the LCM of two numbers in C using function.

Max.c

#include <stdio.h> #include <conio.h> int get_lcm( int a, int b); // function declaration int main() { int n1, n2, lcm; // declaration of variables printf (" \n Enter any two positive numbers to get the LCM of: \n "); scanf ("%d %d", &n1, &n2); lcm = get_lcm( n1, n2); // function calling printf ( " \n LCM of %d and %d is %d. ", n1, n2, lcm); return 0; } int get_lcm ( int n1, int n2) // function definition { /* static variabe is iniatialized only once for each function call */ static int max = 1; if ( max % n1 == 0 && max % n2 == 0) { return max; } else { max++; get_lcm( n1, n2); return max; } } </conio.h></stdio.h>

Output

Enter any two positive numbers to get the LCM of: 30 25 LCM of 30 and 25 is 150.

LCM of two numbers using recursive function

Let's consider a program to get the LCM of two numbers in C using the Recursive function.

Lcm_fun.c

#include <stdio.h> #include <conio.h> // use Recursive function to return gcd of two numbers num1 and num2. int gcd( int num1, int num2) { if ( num1 == 0) // if num1 is equal to 0, return num2 { return num2; } return gcd (num2 % num1, num1); } // lcm_fun () function returns the LCM of two numbers int lcm_fun( int num1, int num2) { // divide the num1 by gcd() function and then multiply with num2. return ( num1 / gcd(num1, num2)) * num2; } int main() { // declaration and initialization of positive numbers int num1, num2; printf( "Enter any two positive numbers \n"); scanf(" %d %d", &num1, &num2); printf ( " LCM of two numbers %d and %d is %d ", num1, num2, lcm_fun( num1, num2)); return 0; } </conio.h></stdio.h>

Output

Enter any two positive numbers 26 20 LCM of two numbers 26 and 20 is 260

Next Topicwhile loop vs do-while loop in C