Task32 - For Loops With Logical Operators

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

import java.util.

Arrays;

import java.util.Scanner;

public class Task32_ForLoopswithLogicalOperators {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int[] array = new int[10];

System.out.println("Enter 10 numbers for the array:");

for (int i = 0; i < array.length; i++) {

array[i] = scanner.nextInt();

System.out.println("Original Array: " + Arrays.toString(array));

int largest = array[0];

int smallest = array[0];

int sum = 0;

for (int num : array) {

if (num > largest) largest = num;

if (num < smallest) smallest = num;

sum += num;

double average = sum / (double) array.length;

System.out.println("Largest Number: " + largest);

System.out.println("Smallest Number: " + smallest);


System.out.println("Sum of Elements: " + sum);

System.out.println("Average of Elements: " + average);

System.out.print("Enter a number to search for: ");

int searchNum = scanner.nextInt();

int index = -1;

for (int i = 0; i < array.length; i++) {

if (array[i] == searchNum) {

index = i;

break;

if (index != -1) {

System.out.println("Number " + searchNum + " found at index " + index + ".");

} else {

System.out.println("Number " + searchNum + " not found in the array.");

System.out.print("Enter an index to update (0-9): ");

int updateIndex = scanner.nextInt();

System.out.print("Enter a new value: ");

int newValue = scanner.nextInt();

array[updateIndex] = newValue;

System.out.println("Updated Array: " + Arrays.toString(array));

Arrays.sort(array);
System.out.println("Sorted Array: " + Arrays.toString(array));

int[] reversedArray = new int[array.length];

for (int i = 0; i < array.length; i++) {

reversedArray[i] = array[array.length - 1 - i];

System.out.println("Reversed Array: " + Arrays.toString(reversedArray));

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy