/************************************************
Grundlegende Algorithmen mit Java,
http://algorithmen-und-problemloesungen.de/
Copyright @2007-2008 by Doina Logofatu
************************************************/

import java.io.*;
import java.util.*;


public class P02Number4 {

  private static final String FileInputName = "nr4.in";

  private static final String FileOutputName = "nr4.out";

  /**
   * Appends in the given string builder the way from a given number to 4
   * @param number the number
   * @param out the string builder
   * @return the string builder
   */
  private static StringBuilder numberFour(int number, StringBuilder out) {

    if (number != 4) {
      switch (number % 10) {
      case 0:
      case 4:
        numberFour(number / 10, out);
        break;
      default:
        numberFour(number * 2, out);
      }
      out.append("->");
    }
    out.append(number);
    return out;
  }

  /**
   * Reads the numbers from the file {@value #FileInputName} 
   * and writes their way to the number 4 in the file {@value #FileOutputName} .
   * @param args
   * @throws IOException 
   */
  public static void main(String[] args) throws IOException {
    Scanner sc = null;
    PrintStream out = null;
    try {
      out = new PrintStream(new File(FileOutputName));
      sc = new Scanner(new File(FileInputName));
      while (sc.hasNextInt()) {
        int number = sc.nextInt();
        out.println(numberFour(number, new StringBuilder()));
      }
    } finally {
      if (sc != null) {
        sc.close();
      }
      if (out != null) {
        out.close();
      }
    }
  }
}

