Decimal

import java.util.*; public class decimal { public decimal() { System.out.println("Please enter a decimal"); Scanner sc = new Scanner(System.in); double decimal = sc.nextDouble(); String decimalString = " " + decimal; String first = decimalString.substring(0,decimalString.indexOf(".")); String second = decimalString.substring(decimalString.indexOf(".")); double p1dub = 1.0; double p2dub = Double.parseDouble(second); while(p2dub%1 != 0){ p1dub = p1dub * 10; p2dub = p2dub * 10; } int counter = (int)p1dub - 1; while(counter > 1){ if( (p1dub/counter)%1 == 0 &&(p2dub/counter)%1 == 0){ p1dub = p1dub/counter; p2dub = p2dub/counter; } counter--; } if(first.equals("0")){ System.out.print((int)p2dub + "/" + (int)p1dub); } else{ System.out.print(first + " " + (int)p2dub + "/" + (int)p1dub); } } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.