Program Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *a,i,max,min,n;
printf("Enter the size of array\n");
scanf("%d",&n);
a=(int*) malloc (n*sizeof(int));
printf("Enter the elements\n");
for(i=0;i<=n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(i=0;i<=n;i++)
{
if(max<a[i])
{
max=a[i];
}
else if(min>a[i])
{
min=a[i];
}
}
printf("maximum number is=%d \n minimum number is=%d\n",max,min);
return 0;
}
Hence, when this two statement max=a[0]; min=a[0]; will execute first max amd min are both variable will assigns 12 . when if(max<a[i]) condition will execute inside the loop it check , 12 is less than 47 ? if yes than max will assign 47 either the second condition will execute and min will assign 47 and this procces will continue untill i<=n this condition will false in the loop
Input:
5
Enter the elements
12
47
10
65
89
30
Output:
maximum number is=89
minimum number is=10
No comments:
Post a Comment