Skip to content
doubtly

2 answers

Best answer

Answer: Store the n numbers in an array, take max = a[0] and replace it whenever a larger element is found.

Logic: after one pass through the array, max holds the largest value.

Program:

#include <stdio.h>

int main()
{
    int n, i;
    int a[100], max;

    printf("How many numbers (1 to 100)? ");
    scanf("%d", &n);

    printf("Enter %d numbers: ", n);
    for (i = 0; i < n; i++)
        scanf("%d", &a[i]);

    max = a[0];
    for (i = 1; i < n; i++)
    {
        if (a[i] > max)
            max = a[i];
    }

    printf("Largest number = %d\n", max);

    return 0;
}

Sample output (actual run):

How many numbers (1 to 100)? 5
Enter 5 numbers: 12 45 7 89 23
Largest number = 89
Doubtly · 23 Apr 2022 Report
Was this answer helpful?

Answer: Read n, take the first number as the largest, then compare each next number with it and update the largest when a bigger number is found.

#include <stdio.h>

int main(void)
{
    int n, i, num, max;
    printf("How many numbers? ");
    scanf("%d", &n);

    printf("Enter number 1: ");
    scanf("%d", &max);          /* first number is the largest so far */

    for (i = 2; i <= n; i++) {
        printf("Enter number %d: ", i);
        scanf("%d", &num);
        if (num > max)
            max = num;           /* found a larger number */
    }

    printf("Largest number = %d\n", max);
    return 0;
}

How it works: the first number entered is stored in max. The loop reads the remaining \(n-1\) numbers one by one; whenever a number is greater than max, it replaces max. After the loop, max holds the largest of all n numbers.

Example: n = 5, numbers 12, 45, 7, 60, 33 → output Largest number = 60. Starting with the first number (not 0) makes the program work correctly even when all numbers are negative.

Doubtly AI · 24 Oct 2026 AI-verified · Oct 2026 Report
Was this answer helpful?

Your answer

Log in or create a free account to answer this question.