C Program To Find Two Numbers are Co-Prime or Not

Lets write a C program to check whether two positive numbers entered by the user are Co-Prime numbers / Relative Prime Numbers or not.

Co-Prime numbers / Relative Prime Numbers: Two numbers are said to be co-prime or relative prime numbers if they do not have a common factor other than 1.

OR

Two numbers whose Greatest Common Divisor(GCD) is 1 are known as Co-Prime or Relative Prime Numbers.

Note: User entered numbers(to check for co-prime) do not require to be prime numbers.

Related Read:
C Program To Find Prime Number or Not using While Loop
C Program to Find Factors of a Number using For Loop

Prime Number: Any natural number which is greater than 1 and has only two factors i.e., 1 and the number itself is called a prime number.

Factors of a number: All the numbers which perfectly divide a given number are called as Factors of that number.

Logic To Find If Two Numbers are Co-Prime or Not

If user enters n1 = 8 and n2 = 15;

We find smallest number in these two numbers(8 and 15) and store it inside variable min. So min = 8;

n1 = 8;
n2 = 15;

1st Iteration
count = 2;

( n1 % count == 0 && n2 % count == 0 )
( 8 % 2 == 0 && 15 % 2 == 0 )
( true && false ) = false;

2nd Iteration
count = 3;

( n1 % count == 0 && n2 % count == 0 )
( 8 % 3 == 0 && 15 % 3 == 0 )
( false && true ) = false;

3rd Iteration
count = 4;

( n1 % count == 0 && n2 % count == 0 )
( 8 % 4 == 0 && 15 % 4 == 0 )
( true && false ) = false;

4th Iteration
count = 5;

( n1 % count == 0 && n2 % count == 0 )
( 8 % 5 == 0 && 15 % 5 == 0 )
( false && true ) = false;

5th Iteration
count = 6;

( n1 % count == 0 && n2 % count == 0 )
( 8 % 6 == 0 && 15 % 6 == 0 )
( false && false ) = false;

6th Iteration
count = 7;

( n1 % count == 0 && n2 % count == 0 )
( 8 % 7 == 0 && 15 % 7 == 0 )
( false && false ) = false;

7th Iteration
count = 8;

( n1 % count == 0 && n2 % count == 0 )
( 8 % 8 == 0 && 15 % 8 == 0 )
( true && false ) = false;

Exit For Loop
Since count = 8 and min = 8, control exits for loop.

We’ve checked the complete iteration of for loop and we can see that no common number perfectly divides both user entered numbers n1 and n2 i.e., 8 and 15. So (8, 15) are co-prime numbers or relative prime numbers.

Example for non co-prime number

If n1 = 12 and n2 = 15

Number 3 perfectly divides both n1 and n2 i.e., 12 and 15. So 12 and 15 have 2 common factors i.e., 1 and 3, hence (12, 15) are not co-prime or relative prime numbers.

Video Tutorial: C Program To Check If Two Numbers are Co-Prime or Not


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

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

Source Code: C Program To Find Two Numbers are Co-Prime or Not

#include<stdio.h>

int main()
{
    int n1, n2, min, count, flag = 1;

    printf("Enter 2 positive numbers\n");
    scanf("%d%d", &n1, &n2);

    min = n1 < n2 ? n1 : n2;

    for(count = 2; count <= min; count++)
    {
        if( n1 % count == 0 && n2 % count == 0 )
        {
            flag = 0;
            break;
        }
    }

    if(flag)
    {
        printf("%d and %d are co-prime\n", n1, n2);
    }
    else
    {
        printf("%d and %d are not co-prime\n", n1, n2);
    }

    return 0;
}

Output 1:
Enter 2 positive numbers
8
15
8 and 15 are co-prime

Output 2:
Enter 2 positive numbers
12
15
12 and 15 are not co-prime

Logic To Check If Two Numbers are Co-Prime or Not

User enters 2 positive numbers, we store those numbers in variables n1 and n2 respectively. We find the smallest number in it and store it inside variable min.

We initialize loop counter variable count to 2(as all the numbers are perfectly divisible by 1, so we skip number 1 and start with number 2). We iterate through the for loop until count value is less than or equal to min. We increment the value of count by one for each iteration of for loop.

Inside for loop we check if there are any common factors for n1 and n2.
n1 % count == 0 && n2 % count == 0
If there are any common factors, then those numbers entered by the user are not co-prime / relative prime numbers. If there are no common factors for those two numbers, then they are co-prime or relative prime numbers.

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 GCD and LCM of Two Numbers using Euclidean algorithm

Lets write a C program to find GCD / HCF and LCM of Two user entered Numbers using Euclidean algorithm.

Full Forms

GCD: Greatest Common Divisor.
HCF: Highest Common Factor.
LCM: Least common Multiple.

Related Read:
while loop in C programming

Formula To Calculate LCM

Once we get the GCD, we use the below formula to calculate LCM.

LCM = ( num1 * num2 ) / GCD;

Source Code: C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm

 
#include < stdio.h >

int main()
{
    int num1, num2, gcd, lcm, rem, numerator, denominator;

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

    if(num1 > num2)
    {
        numerator   = num1;
        denominator = num2;
    }
    else
    {
        numerator   = num2;
        denominator = num1;
    }

    rem = numerator % denominator;

    while(rem != 0)
    {
        numerator   = denominator;
        denominator = rem;
        rem         = numerator % denominator;
    }

    gcd = denominator;
    lcm = (num1 * num2) / gcd;

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

    return 0;
}

Output
Enter 2 integer numbers
15
20
GCD of 15 and 20 is 5
LCM of 15 and 20 is 60

C Program To Find GCD and LCM of Two Numbers using Euclidean algorithm


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

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


Logic To Find GCD and LCM of Two Numbers using Euclidean algorithm

We find biggest of the 2 numbers entered by the user. Biggest number is assigned to variable numerator and smaller number is assigned to variable denominator. Now reminder is calculated. If the reminder is not equal to zero, then the value of variable denominator is assigned to variable numerator and the value of variable reminder is transferred to variable denominator and once again reminder is calculated – until reminder is zero. Once reminder is zero, gcd will be present in variable denominator.

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 GCD and LCM of Two Numbers

Lets write a C program to find GCD(Greatest Common Divisor) or HCF(Highest common Factor) and LCM(Least Common Multiple) of 2 user entered integer numbers.

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.
C Program to Find GCD or HCF of Two Numbers

Least Common Multiple(LCM): is the smallest positive integer that is perfectly divisible by the given integer values.
C Program to Find LCM of Two Numbers
Biggest of Two Numbers Using Ternary Operator: C

Logic To Find GCD and LCM of Two Integer Numbers.

We ask the user to enter 2 integer numbers. Next we find the smallest number among the two. Example, if num1 = 2 and num2 = 3. We can’t have any number bigger than 2, which will divide num1 and have reminder as 0.

Further explanation to find GCD or HCF is present in detail in this article: C Program to Find GCD or HCF of Two Numbers

Formula To Calculate LCM

Once we get the GCD, we use the below formula to calculate LCM.

LCM = ( num1 * num2 ) / GCD;

Source Code: C Program To Find GCD and LCM of Two Numbers

 
#include < stdio.h >

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

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

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

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

    lcm = ( num1 * num2 ) / gcd;

    printf("GCD = %d\nLCM = %d\n", gcd, lcm);

    return 0;
}

Output 1:
Enter 2 integer numbers
30
40
GCD = 10
LCM = 120

Output 2:
Enter 2 integer numbers
12
24
GCD = 12
LCM = 24

C Program To Find GCD and LCM of Two Numbers


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

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