OOP-JAVA Practical 16

OOP-JAVA Practical 16
  • Aim: Write a program that prompts the user to enter a decimal number and displays the number in a fraction. Hint: Read the decimal number as a string, extract the integer part and fractional part from the string.
  • Code:
    import java.util.Scanner;
    public class practical16 {
        public static void main(String[] args) {
            Scanner s=new Scanner(System.in);
            System.out.println("Enter Decimal number: ");
            String[] str=s.nextLine().replaceAll(" ", "").split("\\.");
            System.out.println("Integer part of given number : "+str[0]);
            try {
                System.out.println("Fraction part of given number : "+str[1]);
            } catch (Exception e) {
                System.out.println("Fraction part of given number : 0");
            }
        }
    }
  • Output:
    Output of practical 16

Comments