Dremendo Tag Line

Input numbers and print its square in Java

Input - Question 3

In this question, we will see how to input 5 numbers in Java programming using the java input and print the square of each numbers on separate lines. To know more about java input click on the java input lesson.

Q3) Write a program in Java to input five numbers and print the square of each numbers on separate lines.

Program

import java.util.Scanner;

public class Q3
{
    public static void main(String args[])
    {
        int a,b,c,d,e;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 5 numbers");
        a=sc.nextInt();
        b=sc.nextInt();
        c=sc.nextInt();
        d=sc.nextInt();
        e=sc.nextInt();
        System.out.println("Square of " + a + "=" + (a*a));
        System.out.println("Square of " + b + "=" + (b*b));
        System.out.println("Square of " + c + "=" + (c*c));
        System.out.println("Square of " + d + "=" + (d*d));
        System.out.println("Square of " + e + "=" + (e*e));
    }
}

Output

Enter 5 numbers
2
3
4
5
6
Square of 2=4
Square of 3=9
Square of 4=16
Square of 5=25
Square of 6=36
video-poster