The preceding Java code will give resultant rational number of two given numbers.
- import java.io.*;
- class rational
- {
- public rational() {}
- public long gcd(long a, long b)
- {
- if (b == 0) return a;
- else return gcd(b, a % b);
- }
- }
- public class Rational
- {
- public static void main(String args[]) throws IOException
- {
- rational r = new rational();
- long a, b, x, y;
- String str;
- DataInputStream in = new DataInputStream(System. in );
- System.out.print("Enter the 1st value: ");
- str = in .readLine();
- a = Integer.parseInt(str);
- System.out.print("Enter the 2nd value: ");
- str = in .readLine();
- b = Integer.parseInt(str);
- long l = r.gcd(a, b);
- System.out.println();
- System.out.println("The GCD of the given rational number is: " + l);
- x = a / l;
- y = b / l;
- System.out.println();
- System.out.println("The simplified rational number is: " + x + "/" + y);
- }
- }
Thank you, keep learning and sharing.