Using a while loop create a program that will prompts the user for two numbers and then print out a list of all the numbers between the given numbers (inclusive) squared.
Sample Run1
Enter two numbers: 4 8
Output1: List = 16 25 36 49 64
Sample Run2
Enter two numbers: -2 -6
Output2: List = 36 25 16 9 4
Source code
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
System.out.print("Enter two numbers: ");
int num1=in.nextInt();
int num2=in.nextInt();
if(num1>num2){
int temp=num1;
num1=num2;
num2=temp;
}
System.out.print("List = ");
for(int i=num1;i<=num2;i++){
System.out.print((i*i)+" ");
}
}
}
Sample run 1
Sample run 2
Comments
Leave a comment