GTU OOP Program - 16

16). 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

import java.util.Scanner;

class Fraction {
    private int real;
    private int imaginary;

    Fraction(int r, int i) {
        real = r;
        imaginary = i;
    }

    // finding greatest common multiple using recursion
    public long GCM(long a, long b) {
        return b == 0 ? a : GCM(b, a % b);
    }

    public String toString() {
        long GCM = GCM(real, imaginary);
        return real / GCM + "/" + imaginary / GCM;
    }
}

class Program_16 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a deciaml number : ");
        String decimal = input.nextLine();

        // returns index position of decimal point (.)
        int indexofDec = decimal.indexOf(".");
        int len = decimal.substring(indexofDec).length();
        int img = (int) Math.pow(10, len - 1);
        int real = (int) (Double.parseDouble(decimal) * img);
        Fraction fr = new Fraction(real, img);
        System.out.println("The Fraction Number is " + fr);
    }
}

Output
cse gtu, gtu study material for computer engineering students gujarat technological university oop lab manual solution

gtu oop lab manual,

oop GTU Practical,

oop Programs,

Object oriented Programming,

Gtu study material 3140705,

OOP 3140705,

oop practical list,

Java Programming,

gtu oop practical,

oop lab solution gtu

Comments

YouTube

Popular posts from this blog

GTU OOP Program - 18

GTU OS Program - 7

GTU OS Program - 3