Interface Implementation - Pair

 Interface Implementation - Pair 

 Problem Statement: 
You must define the class Node which implements the interface Pair so that program runs successfully.

The class Hello accepts two integers as the input. It prints the HCF of the given two integers as shown in the Example Input/Output section. Then it prints the string value "YES" if the given two integers are co-prime. Else it prints the string value "NO".

 Example Input/Output 1: 
 Input: 
21 22

 Output: 
HCF: 1
YES

Explanation:
The HCF of the given two integers is 1. So the given integers are co-prime.
Hence the output is
HCF: 1
YES

Example Input/Output 2:
Input:
21 27

Output:
HCF: 3
NO

 Note: Max Execution Time Limit: 50 millisecs 

 Solution: 

 Programming Language: JAVA Language 

import java.util.*;
interface Pair {
    public long getHCF(int a, int b);
    public boolean areCoPrime(int a, int b);
}
class Node implements Pair {
    @Override public long getHCF(int a, int b) {
        return a % b == 0 ? b : this.getHCF(b, a % b);}
    @Override public boolean areCoPrime(int a, int b) {
        return this.getHCF(a, b) == 1;}
}    
    public class Hello {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            int a = sc.nextInt();
            int b = sc.nextInt();
            Pair pair = new Node();
            System.out.println("HCF: " + pair.getHCF(a, b));
            System.out.println(pair.areCoPrime(a, b) ? "YES" : "NO");
        }
}

Alter:

import java.util.*;
interface Pair {
    public long getHCF(int a, int b);
    public boolean areCoPrime(int a, int b);
}
class Node implements Pair{
    public long getHCF(int a,int b){
        if(a==0){
            return b;
        }
        return getHCF(b%a,a);
    }
    public boolean areCoPrime(int a ,int b){
        if(getHCF(a,b)==1){
            return true;
        }
        return false;
    }
}
public class Hello {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        Pair pair = new Node();
        System.out.println("HCF: " + pair.getHCF(a, b));
        System.out.println(pair.areCoPrime(a, b) ? "YES" : "NO");
    }
}

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Function decimalToBinary - CTS Pattern

Integer & Alphabet Pair Pattern

Words Starting with Upper Case

Flowchart Logic 047

Occurrences of Sub in Parent

Find Maxima/Minima