Ask Question
7 August, 11:13

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.

+3
Answers (1)
  1. 7 August, 13:09
    0
    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.
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Square a Number This is a practice programming challenge. Use this screen to explore the programming interface and try the simple challenge ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers