Day 14: Find the Largest Number

Objective

Today’s challenge is to write a program that finds the largest number from a list of numbers provided by the user. This exercise will help you practice working with lists (or arrays) and conditional logic.

For example:

  • Input: [4,7,1,9,2]
  • Output: 9

Why This Challenge Is Important

This challenge will teach you:

  1. How to Work with Lists: Learn to process multiple values in a single variable.
  2. Conditional Logic: Use conditions to compare values.
  3. Problem-Solving: Practice solving a common real-world problem efficiently.

Steps to Solve

1. Understand the Problem

  • The user will input a series of numbers.
  • Your program needs to identify the largest number in that list.

2. Approaches to Solve

There are several ways to approach this:

  1. Using a Loop: Iterate through the list and keep track of the largest number.
  2. Using Built-in Functions: Some languages provide functions to find the maximum value in a list (e.g., max() in Python).

3. Input Format

  • Prompt the user to enter numbers separated by spaces (e.g., “4 7 1 9 2”).
  • Convert the input into a list or array for processing.

Code Examples

Python Example

Using a Loop:

# Get input from the user
numbers = input("Enter numbers separated by spaces: ").split()

# Convert input to a list of integers
numbers = [int(num) for num in numbers]

# Find the largest number using a loop
largest = numbers[0]
for num in numbers:
    if num > largest:
        largest = num

print(f"The largest number is: {largest}")

Using max() Function:

# Get input from the user
numbers = input("Enter numbers separated by spaces: ").split()

# Convert input to a list of integers
numbers = [int(num) for num in numbers]

# Find the largest number using max()
largest = max(numbers)

print(f"The largest number is: {largest}")

Java Example

Using a Loop:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Get input from the user
        System.out.print("Enter numbers separated by spaces: ");
        String[] input = scanner.nextLine().split(" ");

        // Convert input to an array of integers
        int[] numbers = new int[input.length];
        for (int i = 0; i < input.length; i++) {
            numbers[i] = Integer.parseInt(input[i]);
        }

        // Find the largest number using a loop
        int largest = numbers[0];
        for (int num : numbers) {
            if (num > largest) {
                largest = num;
            }
        }

        System.out.println("The largest number is: " + largest);
    }
}

JavaScript Example

Using a Loop:

// Get input from the user
let input = prompt("Enter numbers separated by spaces:");
let numbers = input.split(" ").map(Number);

// Find the largest number using a loop
let largest = numbers[0];
for (let num of numbers) {
    if (num > largest) {
        largest = num;
    }
}

console.log(`The largest number is: ${largest}`);

Using Math.max():

// Get input from the user
let input = prompt("Enter numbers separated by spaces:");
let numbers = input.split(" ").map(Number);

// Find the largest number using Math.max
let largest = Math.max(...numbers);

console.log(`The largest number is: ${largest}`);

Edge Cases to Consider

  1. Empty Input: If the user enters no numbers, display an appropriate message (e.g., “No numbers entered.”).
  2. Single Number: If there’s only one number, it should be the largest by default.
  3. Negative Numbers: Ensure the program correctly handles negative values (e.g., the largest of [-1, -3, -7] is -1).

Extensions to Explore

  1. Second Largest Number: Modify the program to find the second largest number in the list.
  2. Input Validation: Add checks to ensure the user inputs only valid numbers.
  3. Largest in a Matrix: Extend the program to find the largest number in a two-dimensional array.

What You’ve Learned

  • How to work with lists or arrays in your chosen language.
  • How to use loops and conditional logic to compare values.
  • The use of built-in functions for efficient problem-solving.