Square a Number This is a practice programming challenge. Use this screen to explore the programming interface and try the simple challenge below. Nothing you do on this page will be recorded. When you are ready to proceed to your first scored challenge, click "Finish Practicing" above. Programming challenge description: Write a program that squares an integer and prints the result. Input: Your program should read lines from standard input. Each line will contain a positive integer. Output: For each line of input, print to standard output the square of the number. Print out each result on a new line.

Respuesta :

Answer:

import java.io.*;

public class Main {

  public static void main(String[] args) throws IOException {

      BufferedReader brObject = new BufferedReader(new InputStreamReader(System.in));

      String str;

      while ((str = brObject.readLine()) != null) {

          int number = Integer.parseInt(str);

          System.out.println(number * number);

      }

  }

}

Explanation:

  • Inside the main method, create an object of BufferedReader class to read lines from standard input.
  • Declare a string and run a while loop until it reaches the end of the input.
  • Inside the while loop convert the string into an integer data type.
  • Finally display the output by squaring the number.

The program is an illustration of loops.

Loops

Loops are used to perform repeated operations

The program

The program in Python, where comments are used to explain each line is as follows

#This gets the line of input

num = input("Number: ")

#This splits the number into list

numList = num.split()

#This iterates through the list

for i in numList:

   #This prints the square of each number

   print(int(i)**2)

Read more about loops at:

https://brainly.com/question/19344465