Answer to Question #253677 in Java | JSP | JSF for mark

Question #253677

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

1
Expert's answer
2021-10-21T16:07:37-0400

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





Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS