Finding the largest and the smallest number in a list or tuple is one of the most common beginner programs. It teaches you how to scan a collection, keep a “record” in a variable, and update it with an if statement. The same idea is used behind every maximum, minimum, high score and lowest price you have ever seen on a screen. In this guide you will learn how to write the program in Python, C and Java, with the logic, dry runs, tested output and an interactive tool that draws the numbers as bars and shows every step.
Type some numbers and watch the program scan them one by one.
Graphical view
Code
Output
What You Will Learn
- The problem statement
- List, tuple and array: what is the difference?
- Program 1: Largest number
- Program 2: Smallest number
- Program 3: Both in one scan
- Other ways: max(), min(), sorting, functions, index and second largest
- Common mistakes, practice questions and FAQ
Problem Statement
Find the largest and the smallest number in a list or tuple.
The user enters some whole numbers. The program scans them and prints the largest number, the smallest number, or both. The interactive tool above accepts 1 to 10 numbers from -999 to 999, and it lets you switch between a list and a tuple.
List, Tuple and Array: What Is the Difference?
All three store several values under one name, and you reach each value with an index that starts at 0. In Python the two main choices are the list and the tuple. C and Java use an array.
| List | Tuple | |
|---|---|---|
| Brackets | [ ] square | ( ) round |
| Can you change an item? | Yes: nums[0] = 10 | No: it gives a TypeError |
| Can you add items? | Yes: append(), insert() | No |
| Speed and memory | Slightly more | Slightly less |
| Typical use | Data that changes | Fixed data, such as a date or a point |
| Works with max() and min()? | Yes | Yes |
The scanning logic is the same for both, because finding the largest number only reads the items and never changes them. That is why one program works for a list and a tuple. In C and Java, a plain array does the same job.
Program 1: Largest Number
You do not need to sort the numbers to find the largest one. Keep one variable, largest, that remembers the biggest number seen so far. Start by assuming the first number is the largest. Then look at every other number: if it is bigger than the record, it becomes the new record.
Logic:
- Read the numbers into
numsand find their count n. - Set
largest = nums[0]. - Loop i from 1 to n − 1. If
nums[i] > largest, setlargest = nums[i]. - After the loop, print largest.
The loop starts at index 1, because index 0 is already stored in largest. Comparing the first number with itself would be wasted work.
Python program
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
n = len(nums)
largest = nums[0]
for i in range(1, n):
if nums[i] > largest:
largest = nums[i]
print("Largest number:", largest)
C program
#include <stdio.h>
int main() {
int n;
printf("How many numbers? ");
scanf("%d", &n);
int nums[100];
printf("Enter the numbers: ");
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
int largest = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] > largest) {
largest = nums[i];
}
}
printf("Largest number: %d\n", largest);
return 0;
}
Java program
import java.util.Scanner;
public class ListProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many numbers? ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.print("Enter the numbers: ");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int largest = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] > largest) {
largest = nums[i];
}
}
System.out.println("Largest number: " + largest);
}
}
Sample output:
Enter numbers separated by spaces: 45 12 78 34 9 Largest number: 78
Another run (with negative numbers):
Enter numbers separated by spaces: -4 -9 -1 -7 Largest number: -1
Dry run for [45, 12, 78, 34, 9] (largest starts at 45):
| i | nums[i] | Comparison | Is it a new largest? | largest after this step |
|---|---|---|---|---|
| 1 | 12 | 12 > 45? | no | 45 |
| 2 | 78 | 78 > 45? | yes | 78 |
| 3 | 34 | 34 > 78? | no | 78 |
| 4 | 9 | 9 > 78? | no | 78 |
The loop ends and the largest number is 78.
Dry run for [-4, -9, -1, -7] (largest starts at -4):
| i | nums[i] | Comparison | Is it a new largest? | largest after this step |
|---|---|---|---|---|
| 1 | -9 | -9 > -4? | no | -4 |
| 2 | -1 | -1 > -4? | yes | -1 |
| 3 | -7 | -7 > -1? | no | -1 |
The loop ends and the largest number is -1.
Notice that the second example works with negative numbers. That is only because the record starts as nums[0]. Many beginners start with largest = 0, and that gives the wrong answer for a list of negative numbers (see the common mistakes below).
Program 2: Smallest Number
The smallest number program is the mirror image. Only two things change: the variable is called smallest, and the comparison uses < instead of >.
Python program
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
n = len(nums)
smallest = nums[0]
for i in range(1, n):
if nums[i] < smallest:
smallest = nums[i]
print("Smallest number:", smallest)
C program
#include <stdio.h>
int main() {
int n;
printf("How many numbers? ");
scanf("%d", &n);
int nums[100];
printf("Enter the numbers: ");
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
int smallest = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] < smallest) {
smallest = nums[i];
}
}
printf("Smallest number: %d\n", smallest);
return 0;
}
Java program
import java.util.Scanner;
public class ListProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many numbers? ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.print("Enter the numbers: ");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int smallest = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] < smallest) {
smallest = nums[i];
}
}
System.out.println("Smallest number: " + smallest);
}
}
Sample output:
Enter numbers separated by spaces: 45 12 78 34 9 Smallest number: 9
Dry run for [45, 12, 78, 34, 9] (smallest starts at 45):
| i | nums[i] | Comparison | Is it a new smallest? | smallest after this step |
|---|---|---|---|---|
| 1 | 12 | 12 < 45? | yes | 12 |
| 2 | 78 | 78 < 12? | no | 12 |
| 3 | 34 | 34 < 12? | no | 12 |
| 4 | 9 | 9 < 12? | yes | 9 |
The loop ends and the smallest number is 9.
Program 3: Both in One Scan
If you need both answers, do not write two separate loops. Keep two records and update both inside the same loop. The list is scanned only once.
Python program
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
n = len(nums)
largest = nums[0]
smallest = nums[0]
for i in range(1, n):
if nums[i] > largest:
largest = nums[i]
if nums[i] < smallest:
smallest = nums[i]
print("Largest number:", largest)
print("Smallest number:", smallest)
C program
#include <stdio.h>
int main() {
int n;
printf("How many numbers? ");
scanf("%d", &n);
int nums[100];
printf("Enter the numbers: ");
for (int i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
int largest = nums[0];
int smallest = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] > largest) {
largest = nums[i];
}
if (nums[i] < smallest) {
smallest = nums[i];
}
}
printf("Largest number: %d\n", largest);
printf("Smallest number: %d\n", smallest);
return 0;
}
Java program
import java.util.Scanner;
public class ListProgram {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many numbers? ");
int n = sc.nextInt();
int[] nums = new int[n];
System.out.print("Enter the numbers: ");
for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}
int largest = nums[0];
int smallest = nums[0];
for (int i = 1; i < n; i++) {
if (nums[i] > largest) {
largest = nums[i];
}
if (nums[i] < smallest) {
smallest = nums[i];
}
}
System.out.println("Largest number: " + largest);
System.out.println("Smallest number: " + smallest);
}
}
Sample output:
Enter numbers separated by spaces: 5 3 9 1 7 Largest number: 9 Smallest number: 1
| Method | Comparisons for n numbers | Passes over the list |
|---|---|---|
| Two separate loops | 2 × (n − 1) | 2 |
| One combined loop | 2 × (n − 1) | 1 |
Both approaches compare the same number of times, but the combined loop reads the data only once. That matters when the data is big or comes from a file or a network.
For n numbers, finding the largest takes exactly n − 1 comparisons, so for 10 numbers it takes 9, and for 1,000 numbers it takes 999. The time grows in a straight line with n, which is written O(n).
Other Ways to Write the Program
Method 4: Python’s max() and min()
Python already has the functions. They work on lists, tuples, sets and even strings. Use them to check your own program, but write the loop version in an exam, because the examiner wants to see the logic.
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
print("Largest number:", max(nums))
print("Smallest number:", min(nums))
Sample output:
Enter numbers separated by spaces: 45 12 78 34 9 Largest number: 78 Smallest number: 9
Method 5: A fixed tuple
When the numbers are already known, write them directly in round brackets. The program cannot change them, which is exactly what a tuple is for.
nums = (45, 12, 78, 34, 9)
print("Tuple:", nums)
print("Largest number:", max(nums))
print("Smallest number:", min(nums))
Sample output:
Tuple: (45, 12, 78, 34, 9) Largest number: 78 Smallest number: 9
Method 6: Sorting
After sorting, the smallest number is first and the largest is last. It is short, but sorting does much more work than needed: it takes about n × log n steps, compared with n − 1 comparisons for the scan. Also, sorted() makes a new list, so the original order stays safe.
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
ordered = sorted(nums)
print("Smallest number:", ordered[0])
print("Largest number:", ordered[-1])
Sample output:
Enter numbers separated by spaces: 45 12 78 34 9 Smallest number: 9 Largest number: 78
Method 7: Using functions
Putting each search in its own function makes the code reusable. The function takes any list or tuple and returns the answer.
def find_largest(items):
largest = items[0]
for x in items:
if x > largest:
largest = x
return largest
def find_smallest(items):
smallest = items[0]
for x in items:
if x < smallest:
smallest = x
return smallest
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
print("Largest number:", find_largest(nums))
print("Smallest number:", find_smallest(nums))
Sample output:
Enter numbers separated by spaces: 45 12 78 34 9 Largest number: 78 Smallest number: 9
Variation: position of the largest number
Sometimes you need to know where the largest number is. Remember the index instead of the value. The value is then nums[pos]. If the largest number appears more than once, this program reports the first position.
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
pos = 0
for i in range(1, len(nums)):
if nums[i] > nums[pos]:
pos = i
print("Largest number:", nums[pos])
print("Found at index:", pos)
Sample output:
Enter numbers separated by spaces: 45 12 78 34 9 Largest number: 78 Found at index: 2
Variation: second largest number
Keep two records. When a number beats the largest, the old largest becomes the second largest. A number that is only bigger than the second largest, and different from the largest, replaces the second one.
nums = list(map(int, input("Enter numbers separated by spaces: ").split()))
largest = float("-inf")
second = float("-inf")
for x in nums:
if x > largest:
second = largest
largest = x
elif x > second and x != largest:
second = x
print("Largest number:", largest)
print("Second largest:", second)
Sample output:
Enter numbers separated by spaces: 45 12 78 34 9 Largest number: 78 Second largest: 45
Common Mistakes
- Starting the record at 0. With
largest = 0and the numbers -4, -9, -1, the program prints 0, which is not even in the list. Always start with the first item. The same problem happens withsmallest = 0for positive numbers. - Starting the record with a made-up big or small number. Writing
smallest = 1000fails if every number is bigger than 1000. Usenums[0]. - Using the wrong comparison sign. Largest needs
>and smallest needs<. Swapping them gives the other answer without any error message. - Running the program on an empty list.
nums[0]fails with an IndexError, andmax([])fails with a ValueError. Check that the list has at least one item. - Forgetting to read the numbers as integers. Without
int, Python compares text. Then “9” is bigger than “78”, because text is compared character by character. - Trying to change a tuple. A line like
nums[0] = 9on a tuple gives a TypeError. Finding the largest does not need to change anything, so you never need that line. - Printing inside the loop. The largest number is known only after every item has been checked. Print after the loop.
Practice Questions
- Find the largest and smallest of three numbers entered by the user, without a list.
- Print the largest and smallest number and also their positions.
- Find the second smallest number in a list.
- Find the difference between the largest and smallest numbers (the range).
- Find the largest word in a list of words, by length.
- Find the largest number in each row of a list of lists (a table).
Frequently Asked Questions
How do you find the largest number in a list in Python?
Use max(nums), or write a loop that keeps a variable called largest and updates it whenever a bigger number is found. Start the variable with nums[0].
How do you find the smallest number in a tuple?
Use min(t), or loop through the tuple with a variable called smallest. A tuple can be read in the same way as a list.
Does the same program work for a list and a tuple?
Yes. The program only reads the items. The only difference is the brackets, square for a list and round for a tuple.
Why do we start with the first element and not with 0?
The first element is a real member of the list, so the record is always a number from the list. Starting with 0 fails for lists that contain only negative numbers.
Why does the loop start from index 1?
Index 0 is already stored in the record. Starting at 1 saves one useless comparison.
How many comparisons does it take to find the largest number?
Exactly n − 1 comparisons for n numbers. There is no faster way for an unsorted list, because every number must be looked at at least once.
What is the time complexity of finding the maximum?
It is O(n), because the time grows in a straight line with the number of items. The space needed is O(1), as only a few variables are used.
Which is better, a loop or sorting?
The loop is better for just the largest or smallest value, because it needs n − 1 comparisons. Sorting needs about n × log n steps, and it is only worth it if you need the whole sorted list anyway.
What happens if the largest number appears twice?
The value is still correct. If you also store the index, the program that uses a strict “greater than” test reports the first position.
How do you find the largest number in an array in C or Java?
Use the same loop. Store the first array element in largest, then loop from index 1 to n − 1 and update largest when an element is bigger. The programs in this post show both languages.