Advanced   Java   Services JAVA-Praktikum Back Next Up Home
Fibonacci (Lösung)
// ------------------------------- Aufgabe 1 ------------------------------- \\
public class KGV
{
   public static void main(String args[])
   {
      for(;;)
      {
         System.out.println("KGV von zwei ganzen Zahlen\n");

         System.out.print("a = ");
         int a = Stdin.intEingabe();

         System.out.print("b = ");
         int b = Stdin.intEingabe();

         if (a*b==0)
         {
            System.out.println("ende");
            break;
         }
         else
         {
            double max = a < b ? b : a ;
            double min = a < b ? a : b ;
            int i;

            for(i=1 ; (i*max)%min != 0 ; i++)
               ;

            System.out.println("kgv = " + i*max + "\n");
         }
      } // end for

   } // end main

}  // end class


// ------------------------------- Aufgabe 2 ------------------------------- \\
public class GGT
{
   public static void main(String args[])
   {
      for(;;)
      {
         System.out.println("GGT von zwei ganzen Zahlen\n");

         System.out.print("a = ");
         int a = Stdin.intEingabe();

         System.out.print("b = ");
         int b = Stdin.intEingabe();

         if (a<0) a=-a;
         if (b<0) b=-b;

         int max = Math.max(a,b);
         int min = Math.min(a,b);

         if (min==0)
         {
            System.out.println("ende");
            break;
         }
         else
         {
            for(int rest=1; rest!=0; )
            {
               rest = max%min ;
               max = min;
               min = rest ;
            }
            System.out.println("ggt = " + max + "\n");
         }
      } // end for

   } // end main

}  // end class
Valid XHTML 1.0 Strict top Back Next Up Home