IS Programming Fundamentals 1 (Lec) Date: September 14, Array
Array An Array is a collection (sequence ) of a fixed number of variables called elements or components, wherein all the elements are of the same data type. dataType [ ] arrayname = new dataType[noElements];
The Statement: Declares and creates the array num cosisting of 5 elements. Each elements are acceed as num[0], num[1], num[2], num[3], num[4]. int [ ] num = new int [5];
import java.util.Scanner; public class arraydemo1 { public static void main(String[]args) { Scanner keyboard=new Scanner(System.in); int noofEl; System.out.println("How many elements of array: "); noofEl = keyboard.nextInt(); int [] num = new int[noofEl]; int i, sum=0, prod=1, ave=0; System.out.println("Enter "+noofEl+" nos: "); for(i=0;i<noofEl;i++) { num[i]=keyboard.nextInt(); sum = sum + num[i]; prod = prod * num[i]; } for(i=0;i<noofEl;i++) System.out.println("Number "+(i+1)+": "+ num[i]); System.out.println("Sum: "+sum); System.out.println("Product: "+prod); System.out.println("Average: "+(sum/noofEl)); }