Computer Science, asked by dubenikhil331, 3 months ago

Solved Problems
ON SINGLE DIMENSIONAL ARRAY
write a program to initialize a single dimensional array of 8 integers print array element along with the indexes of each element and square of each element in three column​

Answers

Answered by LeelaGhosh1
6

Note -

=> Here, I have used java.util.Scanner class to accept 8 integers from the user.

=> Go through all the comments I have put alongside the códing .

Required Program (in java) -

import java.util.Scanner;

public class ArrayOp{

public static void main(String args[]){

Scanner sc = new Scanner(System.in); /*Scanner class object for input.*/

int arr[] = new int[8]; /*int Array of size 8 for inputting and storing the nos.*/

System.out.println("Enter 8 integers one-by-one on different lines -");

for(int i = 0;i<8;i++){ //Loop for input.

arr[i] = sc.nextInt();

}

//Now, to print the three columns.

System.out.println("Element\t\tIndex\t\tSquare"); /* /t is used for leaving a tab of space . */

for(int j = 0; j<8;j++){ //Loop for required output.

System.out.println(arr[j] + "\t\t" + j + "\t\t" + arr[j]*arr[j]);

}

}

}

Output -

=> I have attached the output in the attachment below.

More to know -

=> Escape sequences are some special characters which consist of a backslash followed by another character (usually a letter, inverted comma or backslash) . They convey special meaning to the compiler .

=> Some more examples of escape sequences in java -

  • \n - new line feed (the control moves to a new line)
  • \t - horizontal tab (leaves a tab of space)
  • \v - vertical tab
  • \\ - backslash
  • \' - single inverted comma
  • \" - double inverted comma
  • \f - form feed (clears the terminal window or output screen completely)

Attachments:
Similar questions