Page 261 - PowerPoint Presentation
P. 261
CAVITE STATE UNIVERSITY
TRECE MARTIRES CITY CAMPUS
Department of Information Technology DCIT 111 - Advanced Programming
// Java program to show valid declarations of Arrays
Data_type variable_name = new type[size];
int Array[] = new int[10]; //means you can enter 10 integers { 10, 2, 3, 7, 54, 22, 87, 23, 1, 8}
char Array[] = new char[10]; //means you can enter 10 character { a, b, c, d, e, f, g, h, I, j}
String Array[] = new String[5]; //means you can enter 5 Strings { “Hello”, “Hi”, “Eow”, “zup!” “World”}
float Array[] = new float[4]; //means you can enter 4 floating point number {1.1, 2.3, 31.2, 123.1}
double Array[] = new double[3]; //means you can enter 3 double floating point number {1.12, 23.12, 1.33}
// Array example to sum all the elements of // Example to sum all the elements of array
array public class ArrSum {
public class ArrSum { public static void main(String[]args) {
public static void main(String[]args) {
int arr[]= {2, 4, 6, 1, 3};
int arr[]= new int[5];
int sum=0; for(int i=0; i<arr.length; i++) {
arr[0]=2; sum = sum+arr[i];
arr[1]=4; }
arr[3]=6; System.out.println(“The Sum is: “ +
arr[3]=1; sum);
arr[4]=3; }
for(int i=0; i<arr.length; i++) { }
sum = sum+arr[i];
}
System.out.println(“The Sum is: “ +
sum); OUTPUT:
}
} The Sum is: 16
// Java array example to enter 10 numbers using Scanner
Import java.util.Scanner;
public class TenNum {
public static void main(String[]args) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[10]; // means you have maximum 10 inputs of numbers
System.out.print(“Please enter 10 numbers: “);
for (int i=0; i<arr.length; i++) {
arr[i] = sc.nextInt();
}
System.out.print (“Displaying all the numbers you’ve entered”);
for (int i=0; i<arr.length; i++) { OUTPUT:
System.out.println(arr[i]); Please enter 10 numbers: 1 0 2 9 3 8 4 7 5 6
}
} Displaying all the numbers you’ve entered
1 0 2 9 3 8 4 7 5 6
37