In this tutorial, we will discuss the concept of a C program to find the largest and smallest number in an array
In this topic, we learn how to find the smallest and largest element of an array(Collection of elements)
This program gets “n” number of elements and Enter the elements of the array as input from the user. then, this program finds and displays the smallest and largest elements from the array using for loops.
In this programs, we can see step by step procedure for completion of the program.
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int arr[30];//array declaration int length,i; int min,max; //essential variable declaration printf("Enter the length of the array: "); scanf("%d", &length);//get input from user for length for(i=0; i<length; i++){ printf("Enter element %d: ",i+1);//enter elements for array scanf("%d", &arr[i]); } max=arr[0];//assume first element as largest number min=arr[0];//assume first element as smallest number for(i=0; i<length; i++){//iterate for loop from first index of array if(max<arr[i]){ max=arr[i]; } if(min>arr[i]){ min=arr[i]; } } printf("\nLargest elements is: %d",max); printf("\nSmallest elements is: %d",min); getch();//display result on the screen return 0; }
When the above code is compiled and executed, it produces the following results
Enter the length of the array: 5 Enter elements 1: 45 Enter elements 2: 12 Enter elements 3: 98 Enter elements 4: 65 Enter elements 5: 34 Largest elements is: 98 Smallest elements is: 12
Suggested for you
For loop in C language
Single dimension array in C language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…