java program to display index value

simple java array program

Program:-

Method 1
java program to show index value of array element

class group{
public static void main(String arng[]){
int sum[]=new int[5];
sum[0]= 1;
sum[1]= 4;
sum[2]= 27;
sum[3]= 4;
sum[4]= 148;

System.out.println("index \t data");

for(int i=0; i<5;i++)
System.out.println(i+ "\t"+sum[i]);
}
}

Output:-

index   data
0           1
1           4
2          27
3           4
4         148


Method 2
Java program to display index value in initialized array

class group{
public static void main(String arng[]){
int sum[]={5, 48, 127, 197, 142};

System.out.println("index \t data");

for(int i=0; i<5;i++) System.out.println(i+ "\t"+sum[i]);
}
}

Output:-

index      data
0             5
1             48
2            127
3            197
4            142

Method 3
Java program to display index value of array using user input

import java.util.Scanner;
class group{
public static void main(String arng[]){
int sum[]= new int[5];
Scanner data = new Scanner(System.in);
System.out.println("Enter 5 element of array");
for(int i=0;i<5;i++)
sum[i]=data.nextInt();
System.out.println("index \t data");
for(int i=0; i<5;i++)
System.out.println(i+ "\t"+sum[i]);
}
}

output:-

Enter 5 element of array
52
63
14
2
28
index       data
0              52
1              63
2              14
3              2
4              28


Learn Java:-
Java codes

Java Programs:-
List of Java Programs

Leave reply

Add your comments here

Back to Top