Java Program to Extract Rational Number | IT2305 - Java Programming Lab

AIM:
To write a Java program for the Extraction of the Rational Number in IT2305 - Java Programming Laboratory.

SOURCE CODE:
/**
@author    sourcecodesonline.blogspot.com
@version  1.6.0
*/
import java.util.*;
class RationalOprn
 {
  /** neum is my private which is  numerator*/
  private int neum;
  /** deno is my private which is denominator*/
  private int deno;

 /**
  objective        : tis is getValues method used to get the values from user
  @param no para for tis method
  @return doesnt return anything 
  */ 

 public void getValues()
  {

  System.out.println("Neumerator..");
  Scanner sin=new Scanner(System.in);
   neum=sin.nextInt();
 System.out.println("denominator..");
   deno=sin.nextInt();
 }

 /**
  objective        : tis is findPrimes method used to find the prime nos
  @param x is the only one parameter
   @param x-------int
  @return an integer array
  */ 

 public int[] findPrimes(int x)
 {
  int i,j,g=0,f=0,cnt=0;
  for(i=2;i<=x;i++)
   {

    f=0;
  for(j=2;j<=x;j++)
       if(i%j==0)
        f++;
     if(f==1)
      cnt++;
   }

  int[] a=new int[cnt];
 g=0;
 for(i=2;i<=x;i++)
   {
    f=0;
    for(j=2;j<=x;j++)
       if(i%j==0)
        f++;
     if(f==1)
      {
       a[g]=i;
        g++;
      }
   }

 return a ;
}

 /**
  objective        : tis is simplifyValues method used to simplify the numerator     and denominator
  @param no para for tis method
  @return doesnt return anything 
  */ 

public void simplifyValues()
  {
   int a[];
   if(neum<deno)
        a=findPrimes(neum);
   else
        a=findPrimes(deno);
        int i;
     for(i=0;i<a.length;)
     {
      if(neum%a[i]==0 && deno%a[i]==0)
       {
           neum/=a[i];
           deno/=a[i];
       }
     else
     i++;
   }
 }

 /**
  objective        : tis is putValues  method used to display the numerator and
    denominator
  @param no para for tis method
  @return doesnt return anything 
  */ 

 public void putValues()
    {
      System.out.print( neum + "/" + deno);
    }
 }
 
public class Rational
 {
    /**
  objective        : tis is main method
  @param args[] is the parameter for this method
   @param args[]------String
  @return doesnt return anything 
  */ 

   public static void main(String args[])
    {

       RationalOprn r=new RationalOprn();
       r.getValues();
       r.simplifyValues();
       r.putValues();
     }
  }
Previous
Next Post »

If you still didn't find what you're looking for, you can search this website below: