C Program To Print N Co-Prime Numbers

Lets write a C program to print N co-prime or relative prime numbers. N being the user entered limit for printing the co-prime number pairs.

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 Factors of a Number using For Loop

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

Expected Result

If user inputs limit = 5
Output
1. ( 2, 3 ) are co-prime numbers.
2. ( 3, 4 ) are co-prime numbers.
3. ( 2, 5 ) are co-prime numbers.
4. ( 3, 5 ) are co-prime numbers.
5. ( 4, 5 ) are co-prime numbers.

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

Entire logic is present in our previous video tutorial. Please visit the link and watch the video without fail before going further: C Program To Find Two Numbers are Co-Prime or Not

Video Tutorial: C Program To Print N Co-Prime Numbers


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

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

Source Code: C Program To Print N Co-Prime Numbers

#include<stdio.h>

int main()
{
    int limit, num1, num2 = 3, count, flag, slno = 1;

    printf("How many co-prime numbers you want to print?\n");
    scanf("%d", &limit);

    while(limit)
    {

        for(num1 = 2; num1 <= num2; num1++)
        {
            for(count = 2; count <= num1; count++)
            {
                flag = 1;
                if(num1 % count == 0 && num2 % count == 0)
                {
                    flag = 0;
                    break;
                }
            }

            if(flag)
            {
                printf("%d. (%d, %d) are co-prime numbers.\n", 
                      slno++, num1, num2);
                limit--;

                if(limit == 0)
                {
                    num1 = num2 + 10;
                }
            }

        }

        num2++;
    }

    return 0;
}

Output 1:
How many co-prime numbers you want to print?
10
1. ( 2, 3 ) are co-prime numbers.
2. ( 3, 4 ) are co-prime numbers.
3. ( 2, 5 ) are co-prime numbers.
4. ( 3, 5 ) are co-prime numbers.
5. ( 4, 5 ) are co-prime numbers.
6. ( 5, 6 ) are co-prime numbers.
7. ( 2, 7 ) are co-prime numbers.
8. ( 3, 7 ) are co-prime numbers.
9. ( 4, 7 ) are co-prime numbers.
10. ( 5, 7 ) are co-prime numbers.

Output 2:
How many co-prime numbers you want to print?
14
1. ( 2, 3 ) are co-prime numbers.
2. ( 3, 4 ) are co-prime numbers.
3. ( 2, 5 ) are co-prime numbers.
4. ( 3, 5 ) are co-prime numbers.
5. ( 4, 5 ) are co-prime numbers.
6. ( 5, 6 ) are co-prime numbers.
7. ( 2, 7 ) are co-prime numbers.
8. ( 3, 7 ) are co-prime numbers.
9. ( 4, 7 ) are co-prime numbers.
10. ( 5, 7 ) are co-prime numbers.
11. ( 6, 7 ) are co-prime numbers.
12. ( 3, 8 ) are co-prime numbers.
13. ( 5, 8 ) are co-prime numbers.
14. ( 7, 8 ) are co-prime numbers.

Logic To Print N Co-Prime Numbers

User enters limit value i.e., how many co-prime numbers he or she wants to print. We put that limit inside while loop condition. So while loop code executes repeatedly until limit is not equal to 0. We decrement the value of limit by 1 as and when we print the co-prime numbers.

Inside while loop we write nested for loop. The outer for loop selects the values for num1 and num2. We know that the first co-prime or relative prime number is 2 and 3. So we initialize num1 = 2 and num2 = 3. We keep incrementing the value of num2 by one outside the outer for loop, and iterate the for loop until num1 is less than or equal to num2.

If num2 is 3, then for loop checks for (num1 always starts from 2, till num1 <= num2) – (2, 2), (3, 2).

Next num2 will be 4. So for loop checks for – (2, 4), (3, 4), (4, 4)

Next num2 will be 5. So for loop checks for – (2, 5), (3, 5), (3, 5), (4, 5) ..and so on.

Inside inner for loop we check if the selected numbers present in num1 and num2 are co-prime or not. That logic is present at C Program To Find Two Numbers are Co-Prime or Not.

Note: Since limit– is inside a for loop, even though limit becomes 0 the while loop won’t exit immediately, until the for loop completes its iterations. It avoid that, we make sure the value of num1 is assigned number greater than num2. That way control exits for loop. Since limit is 0, the control even 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 Expert

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

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

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.

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

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

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

Complete logic is present in our previous day video tutorial, please watch it before continuing: C Program To Find Two Numbers are Co-Prime or Not

Related Read:
C Program To Find Prime Number or Not using While Loop
C Program to Find Factors of a Number using For Loop
Biggest of Two Numbers Using Ternary Operator: C
C Program to Find GCD or HCF of Two Numbers

Video Tutorial: C Program To Find If Two Numbers are Co-Prime or Not using Function


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

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

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

#include<stdio.h>

int coprime(int num1, int num2)
{
    int min, count, flag = 1;

    min = num1 < num2 ? num1 : num2;

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

    return(flag);
}

int main()
{
    int n1, n2;

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

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

    return 0;
}

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

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

Note:
coprime() method returns 1 or 0 value. If it returns 1, then the code inside if block gets executed. If it returns 0, then the code inside else block gets executed.

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 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