Sample Program That Uses  Array

Sample Program 1:

File  Name:  Array.java
Description:  This  program  uses  array
Output: Multiplying a number to itself with out using input stream reader
Program:

public  class  Array{
public static void main(String[] args){ int [] A = new int [5];
int [] B = new int [5]; for (int x=0; x<A.length; ++x){

A[x] = Integer.parseInt(args[x]); B[x] = A[x] * A[x];
System.out.println(B[x]);
}
}
}
 

Sample Program 2:

File  Name:  Array2.java
Description: This program uses array Output: multiplying a number to itself
Program:

import  java.io.*;

public  class  Array2{

public static void main(String[] args) throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr); int[] A = new int[5];
int[] B = new int[5]; String s;
for(int x=0; x<A.length;++x){ System.out.print("Enter" + (x+1) + "number: ");

s  =  br.readLine();
A[x]  =  Integer.parseInt(s);
}
System.out.println();

for(int x=0;x<A.length;++x){ B[x] = A[x] * A[x]; System.out.println(B[x]);

}
}
}
 

Sample Program 3:

File  Name:  Array3.java
Description: This program combined InputStreamReader and BufferedReader in one line
Output:  multiplying  a  number  to  itself
Program:

import  java.io.*;

public  class  Array3{

public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int[] A = new int[5]; int[] B = new int[5]; String s;
for(int x=0; x<A.length;++x){ System.out.print("Enter " + (x+1) + " number: ");
A  [x]  =  Integer.parseInt(br.readLine());

}
System.out.println();

for(int x=0;x<A.length;++x){ B[x] = A[x] * A[x]; System.out.println(B[x]);

}
}
}

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.