Day 5 - 19 August, 2024

Program 16: WAP to store five numbers in an array and print numbers

import java.util.*; class P1 { public static void main(String[] args) { int[] arr = new int[5]; int i, j; Scanner sc = new Scanner(System.in); System.out.println("Enter Five Numbers : "); for (i = 0; i < 5; i++) { arr[i] = sc.nextInt(); } System.out.print("Here is the array : "); for (i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } } } Copy Code
Expected Output:
Input:
Enter Five Numbers : 10 20 30 40 50

Output:
Here is the array : 10 20 30 40 50

Program 17: WAP to find sum and average of 10 numbers using array

import java.util.*; class P2 { public static void main(String[] args) { int[] arr = new int[10]; int i, sum = 0; double avg; Scanner sc = new Scanner(System.in); System.out.println("Enter 10 Numbers : "); for (i = 0; i < 10; i++) { arr[i] = sc.nextInt(); sum += arr[i]; } avg = (float) sum / 10; System.out.print("Sum of 10 Numbers is " + sum + " and average is " + avg); } } Copy Code
Expected Output:
Input:
Enter 10 Numbers : 1 2 3 4 5 6 7 8 9 10

Output:
Sum of 10 Numbers is 55 and average is 5.5

Program 18: WAP to store five numbers in list and display numbers in sorted order

import java.util.*; class P3 { public static void main(String[] args) { int arr[] = new int[5]; int i; System.out.println("Enter 5 Numbers : "); for (i = 0; i < 5; i++) { arr[i] = sc.nextInt(); } Arrays.sort(arr); System.out.print("Here is the sorted array : "); for (i = 0; i < 5; i++) { System.out.print(arr[i] + " "); } } } Copy Code
Expected Output:
Input:
Enter 5 Numbers : 5 3 8 1 7

Output:
Here is the sorted array : 1 3 5 7 8