Ask Question
6 September, 03:19

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.

+2
Answers (1)
  1. 6 September, 03:58
    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?
Find 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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers