Class 10 ICSE - Java Array Programs
Class 10th Java aims to empower students by enabling them to build their own applications introducing some effective tools to enable them to enhance their knowledge, broaden horizons, foster creativity, improve the quality of work and increase efficiency.
It also develops logical and analytical thinking so that they can easily solve interactive programs. Students learn fundamental concepts of computing using object oriented approach in one computer language with a clear idea of ethical issues involved in the field of computing
Class 10th java topics includes revision of class 9th, constructors, user-defined methods, objects and classes, library classes , etc.
Program 1: Write a program to store the marks in three subject and print the marks of three students .
Solution:
import java.util.*; class marks { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int[] marks = new int[3]; //Declaration and memory allocation System.out.println("Enter the marks"); for(int i=0;i< 3;i++) { marks[i]= sc.nextInt(); } System.out.println(" Marks in three subjects are : ") ; for(int i= 0; i < 3; i++) { System.out.print(marks[i] + " ") ; } } }
In the above example, we have created a array of size 3 and displaying the marks stored in it.
Program 2: Write a program to input age of three persons and display their ages through loop.
Solution:
class main1 { public static void main(String[] args) { // create an array int[] age = {12, 4, 5}; // loop through the array // using for loop System.out.println("Using for Loop:"); for(int i = 0; i < age.length; i++) { System.out.println(age[i]); } } }
In the above example, we have created a array of size 3 and displaying the ages through for loop.
Program 3: Write a program to store 10 numbers in an array and display their sum and average.
Solution:
class sumavg { public static void main(String[] args) { int[] numbers = {2, -9, 0, 5, 12, -25, 22, 9, 8, 12}; double sum = 0; double average; // access all elements using for loop // add each element in sum for(int i=0;i< 10;i++) { sum = sum + numbers[i]; } total number of elements int length = numbers.length; calculate the average convert the average from int to double average = (sum / length); System.out.println(" Sum = " + sum); System.out.println(" Average = " + average); } }
In the above example, we have created a array of size 10 and displaying their sum and average
Program 4: Write a program to enter elememts in an array and display them in reverse order.
Solution:
class rev { public static void main(String args[]) { int[] arr = {1,2,3,4,5,6,7,8,9,10}; //creating an array for (int i=9;i>=0;i--) { System.out.print(arr[i] + " "); } } }
In the above example, we have created a array of size 10 and displaying its elements in reverse order
Program 5: Write a program to enter four elements in an array and print the factorial of each number you have entered.
Solution:
class fact { public static void main(String args[]) { int f=1; int[] arr = {2,3,4,5}; for(int i=0;i< 4;i++) { int temp = arr[i]; for(int j= 1;j <= temp;j++) { f = f*j; } System.out.println(" Factorial of array elements are =" +f); f = 1; } } }