✓ Link copied to clipboard
C 📅 July 11, 2026 ⏱ 5 min read

C Program to Find Largest Number Using Dynamic Memory Allocation

C Program to Find Largest Number Using Dynamic Memory Allocation

When you’re working with lists of numbers in C, dynamic memory allocation is super handy. It lets you grab just the right amount of memory you need when your program runs. Let’s check out how to use dynamic memory allocation to find the biggest number in a list of integers. Say you have 5 numbers: 10, 25, 7, 15, and 30. Our program will show that 30 is the biggest.

Prerequisites for the C Program to Find Largest Number

Before diving into the code, you'll need to know a few things.

  • How to use malloc for dynamic memory allocation
  • Basic understanding of pointers and arrays in C
  • Using loops for iteration
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler

Source Code of C Program to Find Largest Number Using Dynamic Memory Allocation

Below is the complete C program using dynamic memory allocation to find the largest number in an array.

largest_number.cC▶ Run
#include <stdio.h>
#include <stdlib.h>

int main() {
    int n, i, largest;
    int *arr;

    // Read the number of elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    // Memory allocation for n elements
    arr = (int *)malloc(n * sizeof(int));

    // Read n elements
    printf("Enter %d elements: ", n);
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Finding the largest number
    largest = arr[0];
    for (i = 1; i < n; i++) {
        if (arr[i] > largest)
            largest = arr[i];
    }

    printf("The largest number is %d\n", largest);

    // Freeing the allocated memory
    free(arr);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Find Largest Number Program

Here's what to expect when you run the program. It asks for the total count, then each number, and finally prints the largest number.

OutputText▶ Run
Enter the number of elements: 5
Enter element 1: 10
Enter element 2: 25
Enter element 3: 7
Enter element 4: 15
Enter element 5: 30
The largest number is 30
Output of C Program to Find Largest Number Using Dynamic Memory Allocation
Output of C Program to Find Largest Number Using Dynamic Memory Allocation

Algorithm to Find the Largest Number in C

In this program, dynamic memory allocation helps manage the size of input at runtime. Here’s the detailed step-by-step approach we follow.

  1. Read the number of elements n from the user.
  2. Allocate memory for n integers using malloc.
  3. Input all n numbers into the allocated array.
  4. Assign the first element of the array to largest.
  5. Loop through the array and compare each element with largest.
  6. If an element is greater than largest, update largest.
  7. After the loop, print largest as the result.
  8. Free the allocated memory.
flowchart TD
  A([Start]) --> B[/Enter the number of elements/]
  B --> C["Allocate memory dynamically for n elements"]
  C --> D[/Enter n elements/]
  D --> E[largest = arr[0]]
  E --> F["i = 1"]
  F --> G{"i < n?"}
  G -->|Yes| H{"arr[i] > largest?"}
  H -->|Yes| I[largest = arr[i]]
  H -->|No| J
  I --> J["i++"]
  J --> G
  G -->|No| K([End])
Flowchart: the algorithm step by step

How the C Program to Find Largest Number Works

Let's break down the code to understand its working better. Each key part is explained below.

  • The #include <stdlib.h> is crucial for dynamic memory functions like malloc.
  • int *arr declares a pointer to store the address of dynamically allocated memory.
  • scanf("%d", &n) reads the number of integers.
  • The line arr = (int *)malloc(n * sizeof) allocates memory for n integers.
  • A loop with for (i = 0; i < n; i++) takes user input to fill the array.
  • Initially, the first array element is stored in largest.
  • Another loop checks if any array element is greater than largest and updates if needed.
  • free(arr) is used to release the allocated memory once its job is done.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What function is used for dynamic memory allocation in C?

🎉Correct! 'malloc' is used for dynamic memory allocation.
🤔Not quite. Think about which function allocates memory.

Time and Space Complexity of the C Program

Understanding the efficiency of the program is crucial, especially when working with dynamic memory.

Time Complexity: O(n). The loop iterates through the array once, with for (i = 0; i < n; i++) handling each element. If you input 10 elements, there are exactly 10 comparisons — 100 elements would give 100 comparisons.

Space Complexity: O(n). The space is required for storing n integers in the array arr. The variables n, i, and largest all are simple integer types.

⚠️

Common mistake:Forgetting to free allocated memory with free can lead to memory leaks.

Key takeaway:Dynamic memory allocation is key for handling variable input sizes in C, but always manage the memory with care!

FAQs About Finding Largest Number Using Dynamic Memory Allocation

Here are some common questions students have about this program.

It lets you allocate just enough memory for the input, saving resources.
If you don’t use free, it can lead to memory leaks, which waste resources.
Yep, calloc initializes the allocated memory to zero, unlike malloc.
Not always. It's handy when the size of data isn’t known at compile time.
It makes sure the right amount of memory is allocated for each data type.

If you found this program helpful, check out these related programs:

Conclusion

Dynamic memory allocation is a powerful way to handle different data sizes in C. You gotta avoid common mistakes like memory leaks by freeing your pointers after you're done. Once you get the hang of this, try writing a program that sorts data using similar ideas. Managing memory well now will save you time and headaches later.

Test your C skills more with our C language quiz, or practice in the online compiler.