How to Determine A Vulgar Fraction In Java

0
19
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

Vulgar Fraction In Java

A vulgar fraction:

A vulgar fraction is a fraction in which denominator is an integer other than 10,100,1000…..
like -3/4,4/5,5/7 etc.

Logic to write a vulgar fraction determination

Well the logic is…

  • Take the fraction in x/y form from user
  • It will come as a string
  • Split the String with “/”
  • Find out the second number
  • Check if that is any multipler of 10.While we modulo operation with 10 ,it should not give any remainder
  • Determine the vulger fraction.

Lets code

 

  • Take the fraction in x/y form from user—–take the input the code is here
  • It will come as a string
  • Split the String with “/”
  • Find out the second number
  • Check if that is any multipler of 10.While we modulo operation with 10 ,it should not give any remainder
  • Determine the vulger fraction.

The code:

 


public class Vulger {
 public Vulger() {
  int deno;
  // this is where you take user input
  String fraction = "3/1000";
  String[] temp = null;
  //Split it with "/"
  temp = fraction.split("/");
  //Change it integer from String
  deno = Integer.parseInt(temp[1]);
  //Here the main logic comes checking if it is divisible by 10 and making
  // sure denominator is only  a subset of  multiplier of 10 like
  // 10,100,1000.... not 20,30,40...etc
  if (((deno % 10) == 0) && (checkval(deno))) {
   System.out.println(fraction + " is not a vulger fraction");
  } else
   System.out.println(fraction + " is a vulger fraction");
 }
 public static void main(String[] Args) {
  new Vulger();
 }
Vulgar Fraction

private boolean checkval(int deno) {
//making sure denominator is only a subset of multiplier of
//10 like 10,100,1000…. not 20,30,40…etc
int res = 1;
do {
res = deno % 10;
deno = deno / 10;
}
while (deno >= 1);
if (res == 1) {
return true;
} else
return false;
}
}

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here