Pages

Thursday, 2 July 2015

Largest number

C Program to Find Largest Element of an Array

Coding:

#include<stdio.h>
#include<stdlib.h>
int main()
{
float *a,max;
int n,i;
printf("Please enter total number of elements:");
scanf("%d",&n);
a=(float*)malloc(n*sizeof(float));
for(i=0;i<n;i++)
{
printf("Enter element %d:",i+1);
scanf("%f",&a[i]);
}
max=a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
printf("Maximum value is=%f",max);
return 0;
}

Output:
Please enter total number of elements:5
Enter element 1:10
Enter element 2:40
Enter element 3:12
Enter element 4:8
Enter element 5:15
Maximum value is=40.000000