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

C Program to Find GCD or HCF of Two Numbers

Lets write a C program to find Greatest Common Divisor(GCD) or the Highest Common Factor(HCF) of 2 integer numbers entered by the user.

Related Read:
while loop in C programming
Relational Operators In C
Logical Operators In C

GCD or HCF: Largest Integer that can divide both the numbers without any remainder or with 0 as remainder.

Logic to Find GCD or HCF of Two Numbers

Variable count is initialized to 1. Next, we ask the user to enter 2 integer numbers. We check the smallest of the 2 numbers and we iterate the while loop until variable count is less than or equal to this smallest number.

Inside while loop we keep incrementing the value of count by one for every iteration. We also check if both numbers entered by the user is perfectly divisible by the value present in count. If we get any such number, we store it inside the variable gcd. And after the control exits while loop we print the value present in variable gcd.

Source Code: C Program to Find GCD or HCF of Two Numbers

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, count = 1;

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

    while(count <= num1 && count <= num2)
    {
        if(num1 % count == 0 && num2 % count == 0)
        {
            gcd = count;
        }
        count++;
    }

    printf("GCD or HCF of %d and %d is %d\n", num1, num2, gcd);

    return 0;
}

OR

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, count = 1, small;

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

    small = (num1 < num2)? num1 : num2;

    while(count <= small)
    {
        if(num1 % count == 0 && num2 % count == 0)
        {
            gcd = count;
        }
        count++;
    }

    printf("GCD or HCF of %d and %d is %d\n", num1, num2, gcd);

    return 0;
}

Output 1:
Enter 2 positive integers
24
60
GCD or HCF of 24 and 60 is 12

Here we check the smallest number among the two numbers entered by the user. We iterate through the while loop until value of count is less than or equal to that smallest number of the 2 numbers entered by the user.
Biggest of Two Numbers Using Ternary Operator: C

C Program to Find GCD or HCF of Two Numbers


[youtube https://www.youtube.com/watch?v=4ZbDj6BrM-Q]

YouTube Link: https://www.youtube.com/watch?v=4ZbDj6BrM-Q [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

C Program to Calculate Generic Root of a Number using Mathematical Formula

Lets write a C program to calculate Generic Root of a Number using Mathematical Formula.

Related Read:
C Program to Find Generic Root of a Number
C Program to Calculate Generic Root of a Number using Ternary Operator

For Example: If user input number is 589, then we add all the individual digits of the number i.e., 5 + 8 + 9 = 22. We get 22. Now we add individual digits of number 22 i.e., 2 + 2 = 4. So Generic Root of number 589 is 4.

Generic Root: We keep adding individual digits of a number until we get single digit output.

Formula To Calculate Generic Root

generic_root = 1 + ( (number-1)%9 );

Source Code: C Program to Calculate Generic Root of a Number using Mathematical Formula

 
#include < stdio.h >

int main()
{
    int num, res;

    printf("Enter a number above 10\n");
    scanf("%d", &num);

    printf("Generic Root of %d is %d\n", num, 1+((num-1)%9) );

    return 0;
}

Output 1:
Enter a number above 10
586
Generic Root of 586 is 1

Output 2:
Enter a number above 10
8
Generic Root of 8 is 8

Output 3:
Enter a number
589
Generic Root of 589 is 4

C Program to Calculate Generic Root of a Number using Mathematical Formula


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

YouTube Link: https://www.youtube.com/watch?v=pI0tOD_CrSs [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

C Program to Calculate Generic Root of a Number using Ternary Operator

Lets write a C program to calculate Generic Root of a Number using Ternary Operator or Conditional Operator.

Related Read:
C Program to Find Generic Root of a Number
Ternary Operator / Conditional Operator In C

For Example: If user input number is 65987, then we add all the individual digits of the number i.e., 6 + 5 + 9 + 8 + 7 = 35. We got 35. Now we add individual digits of number 35 i.e., 3 + 5 = 8. So Generic Root of number 65987 is 8.

Source Code: C Program to Calculate Generic Root of a Number using Ternary Operator

 
#include < stdio.h >

int main()
{
    int num, res;

    printf("Enter a number above 10\n");
    scanf("%d", &num);

    printf("Generic Root of %d is %d\n", num, (res = num % 9) ? res : 9 );

    return 0;
}

Output 1:
Enter a number above 10
65987
Generic Root of 65987 is 8

Output 2:
Enter a number above 10
8
Generic Root of 8 is 8

Output 3:
Enter a number above 10
456
Generic Root of 456 is 6

Output 4:
Enter a number above 10
78910
Generic Root of 78910 is 7

Output 5:
Enter a number above 10
5555
Generic Root of 5555 is 2

C Program to Calculate Generic Root of a Number using Ternary Operator


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

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


Logic To Find Generic Root of a Number using Ternary Operator

(result = num % 9) ? result : 9;

OR

(num % 9) ? (num % 9) : 9;

We divide the user input number by 9 and store the remainder inside variable result. If the number is perfectly divisible by 9 or if the result is zero then we output 9 orelse we output the value present in variable result. Variable result will have the Generic Root of the user entered number. We are applying modular division on the user entered number. And we are dividing it by 9 because 9 is the biggest single digit number and by modulo division of number by 9 we get the Generic Root of the number.

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

C Program to Find Generic Root of a Number

Lets write a C program to calculate Generic Root of a user input number.

Generic Root: of a number is sum of all the digits of the number until we get a single-digit output.

Related Read:
while loop in C programming
Calculate Sum of Digits: C Program

For Example: If user input number is 12345, then we add all the individual digits of the number i.e., 1 + 2 + 3 + 4 + 5 = 15. We got 15. Now we add individual digits of number 15 i.e., 1 + 5 = 6. So Generic Root of number 12345 is 6.

Source Code: C Program to Find Generic Root of a Number

 
#include < stdio.h >

int main()
{
    int num, temp, rem, sum = 0;

    printf("Enter a number\n");
    scanf("%d", &num);

    temp = num;

    while(temp > 0)
    {
        rem  = temp % 10;
        sum  = sum + rem;
        temp = temp / 10;

        if(temp == 0)
        {
            if(sum > 9)
            {
                temp = sum;
                sum  = 0;
            }
        }
    }

    printf("Generic Root of %d is %d\n", num, sum);

    return 0;
}

Output 1:
Enter a number
12345
Generic Root of 12345 is 6

Output 2:
Enter a number
123
Generic Root of 123 is 6

Output 3:
Enter a number
15937
Generic Root of 15937 is 7

Output 4:
Enter a number
222222
Generic Root of 222222 is 3

Output 5:
Enter a number
99999
Generic Root of 99999 is 9

C Program to Calculate Generic Root of a Number


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

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


Logic To Find Generic Root of a Number

If user entered number is 1234.
Num = 1234;
temp = 1234;
sum = 0;

Iteration 1
rem = temp % 10;
rem = 1234 % 10;
rem = 4;

sum = sum + rem
sum = 0 + 4;
sum = 4;

temp = temp / 10;
temp = 1234 / 10;
temp = 123;

Iteration 2
rem = temp % 10;
rem = 123 % 10;
rem = 3;

sum = sum + rem
sum = 4 + 3;
sum = 7;

temp = temp / 10;
temp = 123 / 10;
temp = 12;

Iteration 3
rem = temp % 10;
rem = 12 % 10;
rem = 2;

sum = sum + rem
sum = 7 + 2;
sum = 9;

temp = temp / 10;
temp = 12 / 10;
temp = 1;

Iteration 4
rem = temp % 10;
rem = 1 % 10;
rem = 1;

sum = sum + rem
sum = 9 + 1;
sum = 10;

temp = temp / 10;
temp = 1 / 10;
temp = 0;

Now inside while loop we keep checking for the condition temp = 0. Once this condition is true, we check if the value present in sum is greater than 9. i.e., we check if variable sum has double digit number. If that is true, then we copy the number present in variable sum to variable temp and we assign 0 to sum. Now the while loop continues until the variable sum has single digit number.

Once variable sum has single digit number in it, control exits the while loop and we out put the value of sum as Generic Root of the user entered number.

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