C Program to Find Largest Number – Dynamic Memory Allocation

We will write a C program to find largest number using dynamic memory allocation – calloc(). calloc() function is defined under <stdlib.h>

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

   printf("Enter total number of elements(1 to 100): ");
   scanf("%d",&num);

   // allocates the memory for 'num' elements
   data=(float*)calloc(num,sizeof(float));

   if(data==NULL)
   {
      printf("Error! Memory not Allocated.");
      exit(0);
   }

   printf("\n");

   // store the number entered by the User
   for(i=0;i<num;i++)
   {
      printf("Enter element %d:",i+1);
      scanf("%f",data+i);
   }

   // store largest number at address data
   for(i=0;i<num;i++)
   {
      if( *data < *(data+i))
         *data = *(data+i);
   }

   printf("Largest Element = %.2f",*data);

   return 0;
}

Output:-

Enter total number of elements(1 to 100): 5
Enter element 1:10.2
Enter element 2:23
Enter element 3:23.3
Enter element 4:24
Enter element 5:32
Largest Element = 32.00

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Similar C programs

Leave a Comment

Your email address will not be published. Required fields are marked *