Home »
Code Examples »
C Code Examples
C – Find Largest Number – Dynamic Memory Allocation Code Example
The code for Find Largest Number – Dynamic Memory Allocation
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i, number;
float *var;
printf("Enter total number of elements(1 to 100): ");
scanf("%d",&number);
// allocates the memory for 'number' elements
var=(float*)calloc(number,sizeof(float));
if(var==NULL)
{
printf("Error! Memory not Allocated.");
exit(0);
}
printf("\n");
// store the number entered by the User
for(i=0;i<number;i++)
{
printf("Enter element [%d]: ",i+1);
scanf("%f",var+i);
}
// store largest number at address var
for(i=0;i<number;i++)
{
if( *var < *(var+i))
*var = *(var+i);
}
printf("Largest Element = %.2f",*var);
return 0;
}
/*
Output
Enter total number of elements(1 to 100): 3
Enter element [1]: 1.23
Enter element [2]: 4.67
Enter element [3]: 1.09
Largest Element = 4.67
*/
Code by IncludeHelp,
on August 04, 2022