**C program to find maximum and minimum element in an array** is a program to find the minimum and maximum value in an array. First, we take a number from the user. Then take min and max variable. and assign first value in the array to min and max variable. We read the array one by one and check the number whether array value is greater than max then copy that value to the max and check if it is less than mine then copy that number to min. And at the end print the min and max value which are the minimum and maximum number in an array.

**Concept you need….**

**Algorithm to find minimum and maximum in array**

1. Start

2. Declare variables

3. Take a numbers input from user

4. Assign first array value to min and max

5. Traverse array start to end

6. If array value is greater than max then copy array value to max.

7. If array value is less than min then copy array value to min

8. Continue till array end

9. Print the min and max value

10. End

## C program to find maximum and minimum element in array

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include<stdio.h> main() { int i,size,max=0,min=0; printf("Enter size to find largest and smallest of given numbers\n"); scanf("%d",&size); int a[size]; printf("Enter numbers in array\n"); for(i=0;i<size;i++) { scanf("%d",&a[i]); } min=a[0]; max=a[0]; for(i=1;i<size;i++) { if(a[i]>max) { max=a[i]; } if(a[i]<min) { min=a[i]; } } printf("The largest number is %d\n",max); printf("The smallest number is %d\n",min); } |

**Output :**

**Explanation :**

**1. Write preprocessor directives in c**

**2. Start with the main function**

**3. Declare variable**

i => temporary variable

min => to store min value

max => to store max value

a => to take numbers input

size => total numbers

**4. Take a size of array that is total numbers.**

printf(“Enter size to find largest and smallest of given numbers\n”);

scanf(“%d”,&size);

=> size = 5

**5. Assign size to array a**

a[size];

**6. Take a numbers input from user**

printf(“Enter numbers in array\n”);

for(i=0;i<size;i++) => to take 5 input

scanf(“%d”,&a[i]) => take input using scanf

=> 10, 4, 15, 2, 12

**7. Assign first array value to min and max**

min=a[0] => min = 10

max=a[0] => max = 10

Next traverse the array from start to end

for(i=1;i<size;i++) => using for loop traverse

**At iteration 1 :**

i = 1, 1 < 5 this condition becomes true and for loop executed

if(a[i]>max) => 4 > 10 this condition becomes false and this if block will not executed

if(a[i]<min) => 4 < 10 this condition becomes true and this if block executed

min=a[i] => min = 4

**At iteration 2 :**

i = 2, 2 < 5 this condition becomes true for loop executed

if(a[i]>max) => 15 > 10 this condition becomes true and this if block will executed

max=a[i] => max = 15

if(a[i]<min) => 15 < 4 this condition becomes false and this if block not executed

**At iteration 3 :**

i = 3, 3 < 5 this condition becomes true for loop executed

if(a[i]>max) => 2 > 15 this condition becomes false and this if block will not executed

if(a[i]<min) => 2 < 4 this condition becomes true and this if block executed

min=a[i] => min = 2

**At iteration 4 :**

i = 4, 4 < 5 this condition becomes true for loop executed

if(a[i]>max) => 12 > 15 this condition becomes true and this if block will executed

max=a[i] => max = 12

if(a[i]<min) => 12 < 2 this condition becomes false and this if block not executed

**At iteration 5 :**

i = 5, 5 < 5 this condition becomes false for loop stops execution

**8. Next print the min and max number**

printf(“The largest number is %d\n”,max);

printf(“The smallest number is %d\n”,min);

=> The largest number is 15

=> The smallest number is 2

## Leave a Reply