Problem # 24 : Fractions to Decimals
Write a program that will accept a fraction in the form N/D where N is the numerator and D is the denominator and print out the decimal representation of the fraction. If the decimal representation has a repeating sequence of digits it should be indicated by enclosing it in brackets. For example 1/3 = 0.333333333.... is denoted by 0.(3).
The denominator given will not be 0. Here are some conversions :-
1/3 = 0.(3) 22/5 = 4.4 1/7 = 0.(142857) 3/8 = 0.375 45/56 = 0.803(571428) 11/59 = 0.(1864406779661016949152542372881355932203389830508474576271) 0/1 = 0 22/7 = 3.(142857) 345/112 = 345/112 = 3.0803(571428) 41/333 = 0.(123) 9998/9999 = 0.(9998) 64/4 = 16
Sample Output
Enter fraction : 1/7
1/7 = 0.(142857)
![]() |