Write a C program to store n elements using Dynamic Memory Allocation - calloc() and find the Largest element

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    float *element;

    scanf("%d", &n); //Total number of elements

    // Allocate the memory for 'n' number of elements.
    //Then take the elements as input from test data
int i;
element=(float*)calloc(n,sizeof(float));

           
             for(i=0;i<n;i++) 
    {
             scanf("%f",element+i);
    }
    for(i=1;i<n;++i) 
    {
       if(*element<*(element+i))
           *element=*(element+i);
    }
printf("Largest element = %.2f", *element);
}

input:

5
200
300
500
100
400
output: Largest element = 500.00

No comments:

Post a Comment