Expanding Fractions

In this problem you are to print the decimal expansion of a quotient of two integers. As you well know, the decimal expansions of many integer quotients result in decimal expansions with repeating sequences of digits. You must identify these. You will print the decimal expansion of the integer quotient given, stopping just as the expansion terminates or just as the repeating pattern is to repeat itself for the first time. If there is a repeating pattern, you will say how many of the digits are in the repeating pattern.



There will be multiple input instances; each instance consists of two positive integers on a line. The first integer represents the numerator of the fraction and the second represents the denominator. In this problem, the numerator will always be less than the denominator and the denominator will be less than 1000. Input terminates when numerator and denominator are both zero.



For each input instance, the output should consist of the decimal expansion of the fraction, starting with the decimal point. If the expansion terminates, you should print the complete decimal expansion. If the expansion is infinite, you should print the decimal expansion up to, but not including the digit where the repeated pattern first repeats itself.


For instance, 4/11 = .3636363636…, should be printed as .36. (Note that the shortest repeating pattern should be found. In the above example, 3636 and 363636, among others, are repeating patterns, but the shortest repeating pattern is 36.)


Since some of these expansions may be quite long, multiple line expansions should each contain exactly 50 characters on each line (except the last line, which, of course, may be shorter) – that includes the beginning decimal point.


On the line immediately following the last line of the decimal expansion there should be a line saying either “This expansion terminates. “, or “The last n digits repeat forever. “, where n is the number of digits in the repeating pattern.


Output for each input instance (including the last input instance) should be followed by a blank line.


Helpful hint: The number of digits before the pattern is repeated will never be more than the value of the denominator.


Sample Input

3 7

345 800

112 990

53 122

0 0


Sample Output


The last 6 digits repeat forever.



This expansion terminates.



The last 2 digits repeat forever.




The last 60 digits repeat forever.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;


namespace ConsoleApplication3


    class Program


        static void Main(string[] args)


            int numerator = 53;

            int denominator = 122;

            int remainder = 0;

            string quotient = "0.";

            numerator = numerator * 10;

            while (numerator < denominator)


                numerator = numerator * 10;

                quotient = quotient + "0";


            List<int> pattern = new List<int>();


            int flag = -1;



                int tempQuotient = numerator / denominator;

                quotient = quotient + tempQuotient.ToString();

                remainder = numerator % denominator;

                numerator = remainder * 10;

                if (pattern.Contains(numerator))


                    flag = pattern.IndexOf(numerator);








            } while (remainder != 0);

            if (remainder == 0)



                Console.WriteLine("This expansion terminates.");





                Console.WriteLine("The last " + (pattern.Count – flag) + " digits repeat forever.");





This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s