C program for finding the largest number in an array (dynamic memory allocation)

				
					#include<stdio.h> 
#include<conio.h> 
void main()
{ 
int *ar, i, j, n, L; 
clrscr(); 
printf(“Enter the number of elements in the array”);
scanf(“%d”, &n); 
ar=(int*) malloc(sizeof(int)*n);
for(i=0; i<n; i++) 
{ 
printf(“Enter a number”); 
scanf(“%d”, &ar[i]); 
} 
L=arr[0]; 
for(i=1; i<n;i++) 
{ 
if(arr[i]>L) L=ar[i]; 
} 
printf(“The largest number in the array is : %d”, L); 
getch(); 
}
				
			
Output: 
Enter the number of elements in the array:5
Enter a number: 32
Enter a number: 43
Enter a number: 23
Enter a number: 57
Enter a number: 47
The largest number in the array is : 57

Leave a Reply