This is a c++ program to find the largest and smallest number in an array.
Explanation –
- The user is initially asked to enter the size of the array and it is stored in the variable ‘n’.
- An array ‘arr’ of data type integer is declared with size 10.
- Elements of the array are asked to enter and stored in ‘arr’ using a for loop
- The value at index 0 of arr is assigned to the variable ‘max’.
- Using a for loop and initializing ‘i’ as 0, the largest element is found.
- If max is less than arr[i], then value of arr[i] is assigned to max. i is incremented in every iteration.
- The loop continues till ‘i’ is less than ‘n’.
- Similarly, the smallest element is found.
- The value at index 0 of arr is assigned to the variable ‘min’.
- Using a for loop the smallest element is assigned to min.
- The result is then printed.