SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Primitive Data And Definite Loops
Exercise:
Exercises
Question:2 | ISBN:9780136091813 | Edition: 2

Question

Write a for loop that produces the following output:
1 4 9 16 25 36 49 64 81 100
For added challenge, try to modify your code so that it does not need to use the * multiplication operator. (It can be done! Hint: Look at the differences between adjacent numbers.)

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

public class Test
{

     public static void main(String []args)
     {

         //Use  for loop that produces the output
       for(int i = 1, counter = 3; i <= 100; counter += 2)
       {
            System.out.print(i + " ");
             i += counter;
        }
    }
}

Run Code Result:

1 4 9 16 25 36 49 64 81 100

 

0 0

Discussions

Onlinehelper2020

In the for loop does not use the * multiplication operator to implement the above code as per the question needs.

Post the discussion to improve the above solution.