0% found this document useful (0 votes)
6 views7 pages

Practical 21,22,23,24

The document contains Java programs for various practical exercises, including calculating the average of numbers using arrays, adding elements of an array, sorting an array in ascending order, and reversing an array. Each program is provided with its code and a brief aim statement. The output of each program is also indicated, showcasing the results of the operations performed.

Uploaded by

fackedyug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Practical 21,22,23,24

The document contains Java programs for various practical exercises, including calculating the average of numbers using arrays, adding elements of an array, sorting an array in ascending order, and reversing an array. Each program is provided with its code and a brief aim statement. The output of each program is also indicated, showcasing the results of the operations performed.

Uploaded by

fackedyug
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

SPM

Practical : 21
Aim : Write a Java Program to Calculate average of numbers using Array.
Program:
public class AverageUsingArrays {
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements/numbers");
int num = sc.nextInt();
int[] myArray = new int[num];
System.out.println("Enter the numbers one by one : ");
System.out.println("Press Enter button after each number : ");
for(int i =0; i<num; i++){
myArray[i] = sc.nextInt();
}
double average = 0;
for(int i =0; i<num; i++){
average = average + myArray[i];
}
average = average/num;
System.out.println("Average of given numbers :: "+average);
}
}

Output :

1
SPM

2
SPM

Practical : 22
Aim : Write a Java Program to Add the elements of an Array.
Program :
class Test {
static int arr[] = { 12, 3, 4, 15 };
static int sum()
{
int sum = 0;
int i;
for (i = 0; i < arr.length; i++)
sum += arr[i];
return sum;
}
public static void main(String[] args)
{
System.out.println("Sum of given array is "+ sum());
}
}

Output :

3
SPM

Practical : 23
Aim : Write a Java Program to sort an array in ascending order.
Program :
public class SortAsc {
public static void main(String[] args) {
int [] arr = new int [] {5, 2, 8, 7, 1};
int temp = 0;
System.out.println("Elements of original array: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if(arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

System.out.println();
System.out.println("Elements of array sorted in ascending order: ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

Output :

4
SPM

5
SPM

Practical : 24
Aim : Write a Java Program to reverse an array.
Program :
public class reverseArray {
static void reverse(int a[], int n)
{
int[] b = new int[n];
int j = n;
for (int i = 0; i < n; i++) {
b[j - 1] = a[i];
j = j - 1;
}
System.out.println("Reversed array is: \n");
for (int k = 0; k < n; k++) {
System.out.println(b[k]);
}
}
public static void main(String[] args)
{
int [] arr = {10, 20, 30, 40, 50};
reverse(arr, arr.length);
}
}

Output :

6
SPM

You might also like