w3resource

C Exercises: Find the maximum and minimum element in an array

C Array: Exercise-9 with Solution

Write a program in C to find the maximum and minimum elements in an array.

Pictorial Presentation:

C Exercises: Find the maximum and minimum element in an array

Sample Solution:

C Code:

#include <stdio.h>

void main()
{
    int arr1[100];
    int i, mx, mn, n;
	
	
       printf("\n\nFind maximum and minimum element in an array :\n");
       printf("--------------------------------------------------\n");	

       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);
   
       printf("Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
            {
	      printf("element - %d : ",i);
	      scanf("%d",&arr1[i]);
	    }


    mx = arr1[0];
    mn = arr1[0];

    for(i=1; i<n; i++)
    {
        if(arr1[i]>mx)
        {
            mx = arr1[i];
        }


        if(arr1[i]<mn)
        {
            mn = arr1[i];
        }
    }
    printf("Maximum element is : %d\n", mx);
    printf("Minimum element is : %d\n\n", mn);
}

Sample Output:

Find maximum and minimum element in an array :                                                                
--------------------------------------------------                                                            
Input the number of elements to be stored in the array :3                                                     
Input 3 elements in the array :                                                                               
element - 0 : 45                                                                                              
element - 1 : 25                                                                                              
element - 2 : 21                                                                                              
Maximum element is : 45                                                                                       
Minimum element is : 21

Flowchart:

Flowchart: Find maximum and minimum element in an array

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to count the frequency of each element of an array.
Next: Write a program in C to separate odd and even integers in separate arrays.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP