Computer Science, asked by calypsovaldez46, 8 months ago

How to write a java program to convert decimal number into fraction?

Answers

Answered by bintosep102008
1

#Read the fraction number as a string. Convert it to a BigDecimal.

#Split it into two parts. One will be the numerator and other will be denominator. This is stored in an array.

#To remove the decimal, we need a multiplier of 10 with zeroes equal to the number of digits after the decimal. Keeping this is mind:

i. multiply 10 by the length of digits after decimal. This will be the lenngth of second element of the array in Step 2. This will give us the denominator of the fraction.

ii. Now the numerator can be determined by multiplying the part of the number before decimal (first element of array from Step 2) by the denominator determined in the previous step and adding the part of the number after decimal (second element of array from Step 2).

#This gives us the numerator and denominator parts of our fraction. For example, if the number entered was 2.45, then our algorithm till this step will give us 245/100.

#Now we only need to reduce it to its lowest terms. For this we determine the greatest common divisor  (gcd) of numerator and denominator and divide numerator and denominator (from Step 3) to get the fraction in lowest terms. This code is written in reduceFraction method of the attached class.

pls mark brainliest

Similar questions