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++
// 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 not void coprime(int a, int b) { if ( __gcd(a, b) == 1) cout << "Co-Prime" << endl; else cout << "Not Co-Prime" << endl; } // driver code int main() { int a = 5, b = 6; coprime(a, b); a = 8, b = 16; coprime(a, b); return 0; } |
Java
// Java program to check if two // numbers are co-prime or not 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) 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
# Python3 program to check if two # numbers are co-prime or not # Recursive function to # return gcd of a and b def __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 not def coprime(a, b): if ( __gcd(a, b) == 1): print("Co-Prime") else: print("Not Co-Prime") # Driver code a = 5; b = 6coprime(a, b) a = 8; b = 16coprime(a, b) # This code is contributed by Anant Agarwal |
C#
// C# program to check if two // numbers are co-prime or not using 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 // PHP program to check if two // numbers are co-prime or not // Recursive function to // return gcd of a and b function __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 not function 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 ?> |
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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Length of the longest increasing subsequence such that no two adjacent elements are coprime
- Largest Coprime Set Between two integers
- Check if all the pairs of an array are coprime with each other
- Finding a Non Transitive Coprime Triplet in a Range
- Largest number less than or equal to N/2 which is coprime to N
- Composite XOR and Coprime AND
- Print all Coprime path of a Binary Tree
- Find the length of the Largest subset such that all elements are Pairwise Coprime
- Print all distinct Coprime sets possible from 1 to N
- Coprime divisors of a number
- Count all pairs of divisors of a number N whose sum is coprime with N
- Count of integers up to N which are non divisors and non coprime with N
- Check if the given two numbers are friendly pair or not
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Check if a given pair of Numbers are Betrothed numbers or not
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Find two numbers whose sum is N and does not contain any digit as K
- Check if two same sub-sequences exist in a string or not
- Program to check if two strings are same or not
- Check whether two straight lines are orthogonal or not
Improved By : jit_t

