Find the largest number in a series by using pointers in C language

A pointer is a variable that stores the address of another variable. Instead of holding a value directly, it holds the memory address where the value is stored. Pointers use the dereferencing operator (*) to access the value and the address operator (&) to obtain memory addresses.

In this tutorial, we'll learn how to find the largest number in a series using pointers in C programming.

Syntax

datatype *pointer_name;
pointer_name = &variable_name;

Where the asterisk (*) declares a pointer and the ampersand (&) gets the address of a variable.

Algorithm

Here's the algorithm for finding the largest number using pointers −

  • Step 1: Declare integer variables to store numbers

  • Step 2: Declare pointer variables

  • Step 3: Read numbers from user

  • Step 4: Assign addresses of variables to pointers

  • Step 5: Compare values using pointer dereferencing

  • Step 6: Display the largest number

Example 1: Finding Largest Among Three Numbers

This program finds the largest among three numbers using pointers −

#include <stdio.h>

int main() {
    int num1 = 35, num2 = 75, num3 = 12;
    int *p1, *p2, *p3;
    
    // Assign addresses to pointers
    p1 = &num1;
    p2 = &num2;
    p3 = &num3;
    
    printf("Numbers: %d, %d, %d<br>", *p1, *p2, *p3);
    
    // Find largest using pointer comparison
    if (*p1 > *p2) {
        if (*p1 > *p3) {
            printf("Largest number is: %d<br>", *p1);
        } else {
            printf("Largest number is: %d<br>", *p3);
        }
    } else {
        if (*p2 > *p3) {
            printf("Largest number is: %d<br>", *p2);
        } else {
            printf("Largest number is: %d<br>", *p3);
        }
    }
    
    return 0;
}
Numbers: 35, 75, 12
Largest number is: 75

Example 2: Finding Largest in an Array Using Pointers

This example demonstrates finding the largest number in an array using pointer arithmetic −

#include <stdio.h>

int main() {
    int numbers[] = {23, 67, 12, 89, 45};
    int size = 5;
    int *ptr = numbers;  // Point to first element
    int largest = *ptr;
    
    printf("Array elements: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", *(ptr + i));
        
        // Find largest using pointer arithmetic
        if (*(ptr + i) > largest) {
            largest = *(ptr + i);
        }
    }
    
    printf("\nLargest number in array: %d<br>", largest);
    
    return 0;
}
Array elements: 23 67 12 89 45 
Largest number in array: 89

Key Points

  • Pointers store memory addresses, not direct values
  • Use (*) operator to dereference and access the value at the address
  • Use (&) operator to get the address of a variable
  • Pointer arithmetic allows traversing arrays efficiently

Conclusion

Using pointers to find the largest number demonstrates the power of indirect memory access in C. This approach is particularly useful when working with arrays and dynamic memory allocation.

Updated on: 2026-03-15T14:04:43+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements