C Programming Code Examples C > Beginners Lab Assignments Code Examples Program to Find Largest Number Using Dynamic Memory Allocation Program to Find Largest Number Using Dynamic Memory Allocation Depending upon the number of elements, the required size is allocated which prevents the wastage of memory. If no memory is allocated, error is displayed and the program is terminated. #include <stdio.h> #include <stdlib.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."); exit(0); } printf("\n"); // 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) { // Change < to > if you want to find the smallest number if(*data < *(data + i)) *data = *(data + i); } printf("Largest element = %.2f", *data); return 0; }