
//****************************************************
// TITLE: Power.java                                *
// AUTHOR: Kim Skak Larsen                           *
// DATE: 22/9 2001                                   *
//                                                   *
// The first example of a while loop invariant.      *
// The program computes x^p.                         *
//****************************************************

import java.io.*;

public class Power
{
   public static void main (String[] args)
   {
     int x, p, r, q;

     // Example values
     p = 5; x = 2;

     // Precondition: p >= 0

     r = 1; q = p;

     while /*I*/ (q != 0)  // Invariant: r * x^q = x^p
     {
        r = r * x;
        q = q - 1;
     }

    // Postcondition: r = x^p

     System.out.print(r + "\n");

   } // main

} // Power
