A Java program to accept array element from user;
import java.io.*;
public class user_array {
public static void main(String[] args) throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of array: ");
n= Integer.parseInt(br.readLine());
int a[]= new int[n];
for(int i=0;i {
System.out.print("Enter the "+(i+1)+" element of the array: ");
a[i]= Integer.parseInt(br.readLine());
}
for(int i=0;i {
System.out.println("The "+(i+1)+" element of array: "+a[i]);
}
}
}
To run the above code follow following steps:-
import java.io.*;
public class user_array {
public static void main(String[] args) throws IOException
{
int n;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter the length of array: ");
n= Integer.parseInt(br.readLine());
int a[]= new int[n];
for(int i=0;i
System.out.print("Enter the "+(i+1)+" element of the array: ");
a[i]= Integer.parseInt(br.readLine());
}
for(int i=0;i
System.out.println("The "+(i+1)+" element of array: "+a[i]);
}
}
}
To run the above code follow following steps:-
- Save the above code with file name user_array and extension .java
- Open terminal or cmd and move to location of file using cd command.
- To compile type: javac user_array.java
- To run type: java user_array
No comments:
Post a Comment