Thursday, October 23, 2014

Building Java Programs A Back To Basics Approach - 3rd Edition
by Stuart Reges and Marty Stepp

Chapter 2 Exercise 2

Write a for loop that produces the following output:
289 256 225 196 169 144 121 100 81
Try to not use multiplication operator(*). It can be done by finding a relation of differences between adjacent numbers. 

/* Copyright Mihai George Forlafu @ 2014
 * Copenhagen School of Design and Technology
 * Building Java Programs A Back To Basics Approach 5th edition
 * Chapter 2 Exercise 2, page 156
 */
package ch2_ex2;

public class Ch2_Ex2 
{
    public static void main(String[] args) 
    {
        int cons = 17;
        int count = 64;
        for (int i = 0; i <= 16; i = i+2)
        {
            count = count + cons + i;
            System.out.println(count);
        }
    
    }
    
}

0 comments:

Post a Comment