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

Args Array Largest Array Largest

Uploaded by

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

Args Array Largest Array Largest

Uploaded by

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

public class FindLargestValue {

public static void main(String[] args) {


int[] array = {23, 6, 47, 35, 2, 14}; // Example
array
int largest = findLargest(array);
System.out.println("The largest value in the array is: "
+ largest);
}

// Method to find the largest number of the array


public static int findLargest(int[] array) {
int max = array[0]; // Assume the first element
is the largest initially
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i]; // Update max if the current
element is larger
}
}
return max;
}
}

package arrays;
/** Suppose that numbers in an array are of type int.

Write a code segment that will count and output the number of
times the number 42 occurs in the array.
*
*/

public class CountOccurrences {


public static void main(String[] args) {
int[] array = {42, 1, 42, 3, 42, 5, 6}; // Example array
int count = countOccurrences(array, 42);
System.out.println("The number 42 occurs " + count + "
times in the array.");
}

public static int countOccurrences(int[] array, int number)


{
int count = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == number) {
count++;
}
}
return count;
}
}

package arrays;

import java.util.Arrays;
import java.util.Scanner;

public class MarksProcessor {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] marks = new int[6];

// Asking the user for their 6 marks


System.out.println("Please enter your 6 marks:");
for (int i = 0; i < marks.length; i++) {
System.out.print("Mark " + (i + 1) + ": ");
marks[i] = scanner.nextInt();
}

// Sorting the marks


Arrays.sort(marks);

// Finding the highest grade


int highestGrade = marks[marks.length - 1];

// Calculating the average


int sum = 0;
for (int mark : marks) {
sum += mark;
}
double average = (double) sum / marks.length;

// Outputting the results


System.out.println("\nSorted marks: " +
Arrays.toString(marks));
System.out.println("Highest grade: " + highestGrade);
System.out.println("Average mark: " + average);

scanner.close();
}
}

package arrays;

import java.util.Scanner;

public class Phonebook {


public static void main(String[] args) {
// Sample data for the phonebook
String[] names = {"Alice", "Bob", "Charlie"};
String[] phoneNumbers = {"123-456-7890", "234-567-8901",
"345-678-9012"};
int[] ages = {25, 30, 35};

Scanner scanner = new Scanner(System.in);

// Asking the user for a name


System.out.print("Enter a name: ");
String name = scanner.nextLine();

// Searching for the name in the phonebook


boolean found = false;
for (int i = 0; i < names.length; i++) {
if (names[i].equalsIgnoreCase(name)) {
// If the name is found, print the phone number
and age
System.out.println("Phone Number: " +
phoneNumbers[i]);
System.out.println("Age: " + ages[i]);
found = true;
break;
}
}

// If the name is not found, print a message


if (!found) {
System.out.println("Name not found in the
phonebook.");
}
scanner.close();
}
}

You might also like