Super friendly <br/>code & developer communities

Super friendly
code & developer communities

A place for coders and developers to share, learn and grow together.


Java Programming | Square of numbers in arrays

This is a tutorial for square of numbers in arrays. The program is given below that takes numbers from user and saves them in arrays and calculates square of numbers and prints them. The program is not extendable. Go enjoy the program. Lets begin...


Program for square of numbers in arrays.


//import Scanner as we require it.
import java.util.Scanner;

// the name of our class its public
public class ArraySquare {
	//void main
		public static void main (String[] args)
		{
			//declare int
			int i,sq,a[] = new int[10];

			//Declare input as scanner
			Scanner input = new Scanner(System.in);

			//Take input for arrays
			for(i=0;i<5;i++)
			{
			 System.out.println("Enter Number :");
			 a[i] = input.nextInt();
			}

			System.out.println("Square of Numbers :-");
			//print the arrays
			for(i=0;i<5;i++)
			{
				sq = a[i]*a[i];
				System.out.println(sq);
			}
		}
}


Output

Enter Number :
1
Enter Number :
2
Enter Number :
3
Enter Number :
4
Enter Number :
5
Square of Numbers :-
1
4
9
16
25

How does it work
1. You enter the number.
2. The number is saved in respective array.
3. The squares are calculated.
4. The square is printed.

Explanation.
1. Import the Scanner.
2. Declare the class as public
3. Add the void main function
4. Declare input as Scanner.
5. Add a for loop.
6. Add system.out.println() function with the message to enter number.
7. Take the inputs and save it in arrays.
8. Add one more loop to calculate square.
9. Add system.out.println() function to print square.

At the end.
You learnt creating the Java program for Square of numbers in arrays. So now enjoy the program.

P.S. If you have any questions, click here to join our community and feel free to ask any questions.

Share: