Check if two numbers are co-prime or not - GeeksforGeeks

20 Apr, 2021
Two numbers A and B are said to be Co-Prime or mutually prime if the Greatest Common Divisor of them is 1. You have been given two numbers A and B, find if they are Co-prime or not.Examples :  
Input : 2 3
Output : Co-Prime

Input : 4 8
Output : Not Co-Prime
 
 
C++
Java
Python3
C#
PHP
Javascript
// CPP program to check if two// numbers are co-prime or not#include<bits/stdc++.h>using namespace std; // function to check and print if// two numbers are co-prime or notvoid coprime(int a, int b) {         if ( __gcd(a, b) == 1)        cout << "Co-Prime" << endl;    else        cout << "Not Co-Prime" << endl;       } // driver codeint main(){    int a = 5, b = 6;    coprime(a, b);       a = 8, b = 16;    coprime(a, b);           return 0;}
// Java program to check if two// numbers are co-prime or notclass GFG {         // Recursive function to    // return gcd of a and b    static int __gcd(int a, int b)    {        // Everything divides 0        if (a == 0 || b == 0)            return 0;                 // base case        if (a == b)            return a;                 // a is greater        if (a > b)            return __gcd(a-b, b);                         return __gcd(a, b-a);    }         // function to check and print if    // two numbers are co-prime or not    static void coprime(int a, int b) {                 if ( __gcd(a, b) == 1)            System.out.println("Co-Prime");        else            System.out.println("Not Co-Prime");        }         //driver code    public static void main (String[] args)    {        int a = 5, b = 6;        coprime(a, b);                 a = 8; b = 16;        coprime(a, b);    }} // This code is contributed by Anant Agarwal.
# Python3 program to check if two# numbers are co-prime or not # Recursive function to# return gcd of a and bdef __gcd(a, b):     # Everything divides 0    if (a == 0 or b == 0): return 0         # base case    if (a == b): return a         # a is greater    if (a > b):        return __gcd(a - b, b)                 return __gcd(a, b - a) # Function to check and print if# two numbers are co-prime or notdef coprime(a, b):         if ( __gcd(a, b) == 1):        print("Co-Prime")    else:        print("Not Co-Prime")     # Driver codea = 5; b = 6coprime(a, b) a = 8; b = 16coprime(a, b) # This code is contributed by Anant Agarwal
// C# program to check if two// numbers are co-prime or notusing System; class GFG {     // Recursive function to    // return gcd of a and b    static int __gcd(int a, int b)    {        // Everything divides 0        if (a == 0 || b == 0)            return 0;         // base case        if (a == b)            return a;         // a is greater        if (a > b)            return __gcd(a - b, b);         return __gcd(a, b - a);    }     // function to check and print if    // two numbers are co-prime or not    static void coprime(int a, int b) {         if (__gcd(a, b) == 1)            Console.WriteLine("Co-Prime");        else            Console.WriteLine("Not Co-Prime");    }     // Driver code    public static void Main()    {        int a = 5, b = 6;        coprime(a, b);        a = 8;        b = 16;        coprime(a, b);    }} // This code is contributed by Anant Agarwal.
<?php// PHP program to check if two// numbers are co-prime or not // Recursive function to// return gcd of a and bfunction __gcd($a, $b)    {        // Everything divides 0        if ($a == 0 || $b == 0)            return 0;         // base case        if ($a == $b)            return $a;         // a is greater        if ($a > $b)            return __gcd($a - $b, $b);         return __gcd($a, $b - $a);    }     // function to check and print if    // two numbers are co-prime or notfunction coprime($a, $b){    if (__gcd($a, $b) == 1)        echo "Co-Prime","\n";    else        echo "Not Co-Prime","\n";} // Driver Code$a = 5; $b = 6;coprime($a, $b);$a = 8;$b = 16;coprime($a, $b); // This code is contributed by aj_36?>
<script> // Javascript program to check if two// numbers are co-prime or not // Recursive function to// return gcd of a and bfunction __gcd(a, b){         // Everything divides 0    if (a == 0 || b == 0)        return 0;         // Base case    if (a == b)        return a;         // a is greater    if (a > b)        return __gcd(a - b, b);                 return __gcd(a, b - a);} // Function to check and print if// two numbers are co-prime or notfunction coprime(a, b){    if (__gcd(a, b) == 1)        document.write("Co-Prime" + "<br>");    else        document.write("Not Co-Prime");    }  // Driver Codevar a = 5, b = 6;coprime(a, b); a = 8; b = 16;coprime(a, b); // This code is contributed by Kirti </script>
Output : 
Co-Prime
Not Co-Prime
This article is contributed by Dibyendu Roy Chaudhuri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Kirti_Mangal
GCD-LCM
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java