First time FOR loop - can't figure out how to make it loop - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# First time FOR loop - can't figure out how to make it loop

Posts: 1Member
My project is to:

Have user enter a "lower bound" and a "higher bound" for a range of integers that I'm making 0-10.

The code is supposed to automatically calculate and display the squares and the square roots for all numbers in the range.

If the lower bound is greater than the upper bound, the statement "No work is to be performed" should be displayed.
---------------------------------------------

I can make the input work and get the first part to display, but I can't figure out the LOOP part and I can't figure out the if/else to get it to work.

If anyone can help, I would appreciate it. Here is what I have:
------------------------------------------

import java.util.*;

public class P4
{
public static void main(String[] args)
{
int x;
int lowerBound, upperBound;
double square, squareRoot;
Scanner kb = new Scanner(System.in);

for (x = 1; x <= 10; x++);
{
System.out.print("Please enter a lower bound: ");
lowerBound = kb.nextInt();

System.out.print("Please enter an upper bound: ");
upperBound = kb.nextInt();

square = Math.pow(lowerBound,2);
squareRoot = Math.sqrt(lowerBound);

if (lowerBound <= upperBound)
System.out.printf("For the value of " + lowerBound + ", the square is " + "%.2f", +square);
System.out.printf(" and the square root is " + "%.2f%n", +squareRoot);

if (lowerBound > upperBound)
System.out.println("The lower bound is greater than the upper bound.");
System.out.println("No work will be performed.");
}
}}

• Posts: 55Member
edited December 2013

If I understand well the requirements, the user types 1 lower bound and 1 upper bound, and then the code is calculating and displaying the squares and the square roots for all numbers in the range(all numbers between lower_bound and upper_bound). But lower bound must be >=1 and upper bound must be <=10.

`for (x = 1; x <= 10; x++);{ System.out.print("Please enter a lower bound: "); lowerBound = kb.nextInt();

System.out.print("Please enter an upper bound: "); upperBound = kb.nextInt();

if(lowerBound <= upperBound){...}

if(lowerBound > upperBound){...} }`

You ask here the user to type the bounds 9 times.

I will move the the lines where you ask the user to provide input and read it before the LOOP so you will ask the user to type them only once.

I think it's easier and more logical to test from the start if the input is correct.

` import java.util.*;

public class P4{

``````public static void main(String[] args){
int x;
int i=0;
int lowerBound, upperBound;
double square, squareRoot;
int wrong=0;`
//the array has space for 10 elements but the counting starts from 0
`int seq[] = new int[11];`//array with the sequence between lower and upper bounds
`int currNo;
``````

Scanner kb = new Scanner(System.in);

System.out.print("Please enter a lower bound: "); lowerBound = kb.nextInt();

System.out.print("Please enter an upper bound: "); upperBound = kb.nextInt();

`//checking if the input is correct`if (lowerBound > upperBound){ System.out.println("The lower bound is greater than the upper bound."); wrong++; }`

//checking if the input is correct

``````if((lowerBound < 1)||(lowerBound > 10)){
System.out.println("The lower bound is out of limit(1-10).");
wrong++;
}``````

//checking if the input is correct

``````if((upperBound > 10)||(upperBound < 1)){
System.out.println("The upper bound is out of limit(1-10).");
wrong++;
}``````

//if the input is correct `if(wrong == 0){`

``````//squareRoot = Math.sqrt(lowerBound);

//System.out.printf(" and the square root is " + "%.2f%n", +squareRoot);

`x = lowerBound;`

//adding the sequence to the seq array`
while(x<=upperBound){
seq[i] = x;
System.out.println("seq["+i+"]="+x);
x++;
i++;
}

for(i=0; i<=upperBound-lowerBound; i++){
currNo = seq[i];
square = Math.pow(currNo,2);
squareRoot = Math.sqrt(currNo);
System.out.printf("For the value of " + currNo + ", the square is " + "%.2f", +square);
System.out.printf(" and the square root is " + "%.2f%n", +squareRoot);
System.out.println();
}
``````

} `

} }`