C Program to Find LCM of Two Numbers

Lets write a C program to find Least Common Multiple (LCM) of 2 integer numbers.

Related Read:
while loop in C programming
Biggest of Two Numbers Using Ternary Operator: C

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.

Logic to Find LCM of Two Numbers

We assign variable fact with a initial value of 1. While loop keeps executing until fact value is 0. We ask the user to enter 2 integer numbers. Now we find the biggest of these 2 numbers and store it inside variable lcm. We can even skip this step – in which case while loop has to execute more number of times. To optimize the code we find biggest of the 2 user input numbers and store it inside variable lcm – we do this because, we need to find a number which is perfectly divisible by both the user entered number, so obviously the number should be greater than or equal to the biggest number entered by the user.

Next, inside while loop we check if the value present in variable lcm is perfectly divisible by both num1 and num2. Once the condition is true, we display the result on to the console window and assign value 0 to variable fact. Once fact is 0, control exits while loop.

If the value of lcm is not perfectly divisible by both num1 and num2, then we increment the value of variable lcm by 1 and check for divisibility once again. We keep executing the while loop and keep incrementing the value of lcm by 1 until the value present in lcm is perfectly divisibile by value of num1 and num2.

Source Code: C Program to Find LCM of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, lcm, fact =1;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &num1, &num2);

    lcm = (num1 > num2) ? num1 : num2;

    while(fact)
    {
        if(lcm % num1 == 0 && lcm % num2 == 0)
        {
            printf("LCM of %d and %d is %d\n", num1, num2, lcm);
            fact = 0;
        }
        lcm++;
    }

    return 0;
}

Output 1:
Enter 2 numbers
24
60
LCM of 24 and 60 is 120

Output 2:
Enter 2 numbers
2
3
LCM of 2 and 3 is 6

C Program to Find LCM of Two Numbers


[youtube https://www.youtube.com/watch?v=XwSf-rvdkzo]

YouTube Link: https://www.youtube.com/watch?v=XwSf-rvdkzo [Watch the Video In Full Screen.]


Explanation With Example

If 2 numbers are num1 = 2 and num2 = 3.
We find biggest of these two numbers and store it inside variable lcm.
So 3 is greater than 2, so lcm = 3.

Iteration 1
fact = 1;
lcm = 3;

lcm % num1 = 3 % 2 = 1 // not equal to 0
lcm % num2 = 3 % 3 = 0 // equal to 0

Increment value of lcm by 1. So lcm = 4.

Iteration 2
fact = 1;
lcm = 4;

lcm % num1 = 4 % 2 = 0 // equal to 0
lcm % num2 = 4 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 5.

Iteration 3
fact = 1;
lcm = 5;

lcm % num1 = 5 % 2 = 2 // not equal to 0
lcm % num2 = 5 % 3 = 1 // not equal to 0

Increment value of lcm by 1. So lcm = 6.

Iteration 3
fact = 1;
lcm = 6;

lcm % num1 = 6 % 2 = 0 // equal to 0
lcm % num2 = 6 % 3 = 0 // equal to 0

Since both the condition are true
i.e., lcm%num1 == 0 && lcm%num2 == 0

We printout the value of variable lcm i.e., 6 as the Least Common Multiple of numbers 2 and 3. Next we assign 0 to variable fact. Since fact is 0, control exits while loop.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert0

Find First and Second Biggest in N Numbers, without using Arrays: C Program

Write a C program to find first and second biggest numbers in list of N numbers without using arrays and without sorting the list and by using while loop.

Related Read:
while loop in C programming
Find Biggest of N Numbers, without using Arrays: C Program

Source Code: Find First and Second Biggest in N Numbers, without using Arrays: C Program

 
#include<stdio.h>

int main()
{
    int limit, num, fbig = 0, sbig = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d positive numbers\n", limit);
 

    while(limit > 0)
    {
        scanf("%d", &num);

        if(num > fbig)
        {
            sbig = fbig;
            fbig = num;
        }
        if(num > sbig && num < fbig)
        {
            sbig = num;
        }

        limit--;
    }

    printf("First Big is %d\n", fbig);
    printf("Second Big is %d\n", sbig);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
First Big is 5
Second Big is 4

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
First Big is 5
Second Big is 4

Output 3:
Enter the limit
5
Enter 5 numbers
1
4
3
5
2
First Big is 5
Second Big is 4

Output 4:
Enter the limit
8
Enter 8 numbers
1
5
9
3
7
4
6
8
First Big is 9
Second Big is 8

Find First and Second Biggest in N Numbers, without using Arrays: C Program


[youtube https://www.youtube.com/watch?v=gs4YT-Qcw6k]

YouTube Link: https://www.youtube.com/watch?v=gs4YT-Qcw6k [Watch the Video In Full Screen.]


Logic To Find First and Second Biggest Number in N Numbers, without using Arrays

First we ask the user to enter length of numbers list. If user enters limit value as 5, then we ask the user to enter 5 numbers. Once the user enters limit value, we iterate the while loop until limit value is 0. For each iteration of while loop we ask the user to enter a integer number. We check if the new number entered by the user is greater than fbig. If true, we swap the value of fbig to sbig and value of num to fbig. If the new number entered by the user is not greater than fbig, then we check if its greater than sbig. If true, we transfer the value of num to sbig.

Once the value of limit is 0, control exits the while loop, and inside fbig we have first biggest number from the list of numbers entered by the user and sbig will have the second biggest number from the list of numbers entered by the user.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Find Biggest of N Numbers, without using Arrays: C Program

Write a C program to find biggest of N numbers without using Arrays and using while loop.

Related Read:
while loop in C programming

Logic To Find Biggest Number, without using Arrays

We ask the user to enter the limit. i.e., the length of the list of numbers. For Example, if user enters limit value as 5, then we accept 5 numbers from the user and then find the biggest and output that number on to the console window.

First we ask the user to enter the limit. If user enters limit value as 5, we iterate the while loop 5 times i.e., until value of limit is 0. Inside the while loop, for each iteration we ask the user to enter a number. Next we check if that user entered number is greater than the value present in variable big. If the new number entered by the user is greater than value present in variable big, then we copy assign the new number entered by the user to the variable big. We keep doing this for each number entered by the user. Simultaneously we keep decrementing the value of variable limit by one for each iteration of while loop. Once limit is 0, control exits the while loop and we print the value present inside the variable big.

Source Code: Find Biggest of N Numbers, without using Arrays: C Program

 
#include<stdio.h>

int main()
{
    int limit, num, big = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);

    scanf("%d", &num);
    big = num;
    limit = limit - 1;

    while(limit > 0)
    {
        scanf("%d", &num);
        if(num > big)
        {
            big = num;
        }
        limit--;
    }

    printf("Biggest number is %d\n", big);

    return 0;
}

Output 1:
Enter the limit
5
Enter 5 numbers
1
2
3
4
5
Biggest number is 5

Output 2:
Enter the limit
5
Enter 5 numbers
5
4
3
2
1
Biggest number is 5

Output 3:
Enter the limit
5
Enter 5 numbers
1
2
5
3
4
Biggest number is 5

Output 4:
Enter the limit
10
Enter 10 numbers
1
5
9
3
7
5
6
14
1
1
Biggest number is 14

Find Biggest of N Numbers, without using Arrays: C Program


[youtube https://www.youtube.com/watch?v=Cx4-egvgSQs]

YouTube Link: https://www.youtube.com/watch?v=Cx4-egvgSQs [Watch the Video In Full Screen.]


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Biggest of 3 Numbers: C

Lets write C program to find biggest of 3 numbers, using nested if else statement.

Related Read:
Nested if else Statement In C
Relational Operators In C

Biggest of 3 Numbers Using Ternary Operator: C

C Program To Find Biggest of 3 numbers

 
#include < stdio.h >

int main()
{
    int a, b, c;

    printf("Enter 3 numbers\n");
    scanf("%d %d %d", &a, &b, &c);

    if( a > b && a > c)
    {
        printf("%d is biggest\n", a);
    }
    else
    {
        if( b > c )
        {
            printf("%d is biggest\n", b);
        }
        else
        {
            printf("%d is biggest\n", c);
        }
    }

    return 0;
}

Output 1:
Enter 3 numbers
1
2
3
3 is biggest

Output 2:
Enter 3 numbers
10
18
15
18 is biggest

Here first we check if a is greater than b. If yes, then we display a as big. Else b or c is biggest. So now we check if b is greater than c, if yes, we display b as biggest else c is biggest. Nested if else condition works great for programs like this.

Biggest of 3 numbers: C Program


[youtube https://www.youtube.com/watch?v=iy–Q0cLnfc]

YouTube Link: https://www.youtube.com/watch?v=iy–Q0cLnfc [Watch the Video In Full Screen.]


C Program To Find Biggest of 3 numbers

 
#include < stdio.h >

int main()
{
    int a, b, c, big;

    printf("Enter 3 numbers\n");
    scanf("%d %d %d", &a, &b, &c);

    if( a > b && a > c)
    {
        big = a;
    }
    else
    {
        if( b > c )
        {
            big = b;
        }
        else
        {
            big = c;
        }
    }

    printf("Biggest of 3 number is %d\n", big);

    return 0;
}

Output:
Enter 3 numbers
500
550
555
Biggest of 3 number is 555

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

Biggest of Two Numbers Using Ternary Operator: C

Lets find biggest of 2 numbers using ternary operator / conditional operator.

Related Read:
Ternary Operator / Conditional Operator In C

To find biggest of Two numbers using if-else control structure:
Biggest of Two Numbers: C

Source Code: Biggest of Two Numbers using ternary operator: C

 
#include < stdio.h >

int main()
{
    int a, b, big;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    (a > b) ? (big = a) : (big = b);

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
5
6
Biggest of 5 and 6 is 6

Output 2:
Enter 2 numbers
40
15
Biggest of 40 and 15 is 40

 
#include < stdio.h >

int main()
{
    int a, b, big;

    printf("Enter 2 numbers\n");
    scanf("%d%d", &a, &b);

    big = (a > b) ? (a) : (b);

    printf("Biggest of %d and %d is %d\n", a, b, big);

    return 0;
}

Output 1:
Enter 2 numbers
500
900
Biggest of 500 and 900 is 900

In above source code, if a is bigger than b, then value of a is returned and stored in variable big orelse value of variable b is stored in variable big.

General Form of Ternary Operator

(expression_1) ? (expression_2) : (expression_3);

expression_1 is a comparison/conditional argument. expression_2 is executed/returned if expression_1 results in true, expression_3 gets executed/returned if expression_1 is false.

Biggest of 2 Numbers Using Ternary Operator: C


[youtube https://www.youtube.com/watch?v=TB11OWMyiR8]

YouTube Link: https://www.youtube.com/watch?v=TB11OWMyiR8 [Watch the Video In Full Screen.]


For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert