Ask Question

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.

+1
Answers (1)
  1. 9 November, 10:33
    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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers