Java Tutorial
import java.io.*; class rational1 { public rational1(){} 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 { /*** Create a new rational object*/ rational1 r=new rational1(); /*** Numerator and Denominator of Rational Number*/ long a,b,x,y; String str; DataInputStream in= new DataInputStream(System.in); System.out.println("Enter the value1"); str=in.readLine(); a=Integer.parseInt(str); System.out.println("Enter the value2"); str=in.readLine(); b=Integer.parseInt(str); long l=r.gcd(a,b); System.out.println(); System.out.println("GCD of the numbers: "+l); x=a/l; y=b/l; System.out.println(); System.out.println("Rational number: "+x+"/"+y); } }Output:
$java rational Enter the value1 50 Enter the value2 100 GCD of the numbers: 50 Rational number: 1/2
Java Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page