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