C Program To Find Largest Number Using Dynamic Memory Allocation

In this example, we take multiple input from user and find the largest number entered by the user in a dynamically allocated memory.

Find Largest Number Using Dynamic Memory Allocation In C Programming

	
#include <stdio.h>
#include <conio.h>
int main()
{
    int i, j;
    float *data;

    printf("Enter total number of elements(1 to 100): ");
    scanf("%d", &j);
    
    // Allocates the memory for 'j' elements.
    data = (float*) calloc(j, sizeof(float));
    if(data == NULL)
    {
        printf("Error!!! memory not allocated.");
        return 0;
    }

    // Stores the number entered by the user.
    for(i = 0; i < j; ++i)
    {
        printf("Enter Number %d: ", i + 1);
        scanf("%f", data + i);
    }

    // Loop to store largest number at address data
    for(i = 1; i < j; ++i)
    {
        if(*data < *(data + i))
            *data = *(data + i);
    }
    printf("Largest element = %.2f", *data);

    return 0;
}
	

Output :

	
Enter total number of elements(1 to 100): 3
Enter Number 1: 1
Enter Number 2: 5
Enter Number 3: 2
Largest element = 5.00
	

Share your thoughts

Ask anything about this examples