# C Program To Find Largest And Smallest Number In An Array

In this article, we are going to write a c program to find Largest And Smallest Number In An Array.

We will make this program in the following way -:

• C Program To Find Largest And Smallest Number In An Array Using Pointer
• C Program To Find Largest And Smallest Number In An Array Using Recursion
• C Program To Find 2nd Largest And Smallest Number In An Array

To make this program, we will use the following concept given below. If you do not know about these topics well, then you may not understand this program, so you should know about them in detail from the link given below.

Now without wasting time let’s start programming.

Page Contents

## C Program To Find Largest And Smallest Number In An Array Using Pointer

Program -:

``````//C Program To Find Largest and Smallest Number In An Array Using Pointer

#include<stdio.h>
void  main()
{
int i,*ptr, n,a,larg,small;

printf("Enter How many number you want?:\n") ;
scanf("%d",&n) ;

printf("Enter %d numbers\n",n) ;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]) ;
}
ptr = &a;
larg= *ptr;
for(i=0;i<n;i++,*ptr++)
{
if(*ptr>larg)
{
larg=*ptr;
}
}
ptr = &a;
small= *ptr;
for(i=0;i<n;i++,*ptr++)
{
if(*ptr<small)
{
small=*ptr;
}
}
printf("%d is Largest Number\n",larg);
printf("%d is Smallest Number",small);
}
``````

Output -:

``````Enter How many number you want?:
5
Enter 5 numbers
19
28
38
28
27

38 is Largest Number
19 is Smallest Number``````

## C Program To Find Largest And Smallest Number In An Array Using Recusion

Program -:

``````//C Program To Find Largest and Smallest Number In An Array Using Recursion

#include<stdio.h>
int Largest(int a[],int n);
int smallest(int a[],int n);
void main()
{
int i,j,n,a;
printf("Enter a Number :");
scanf("%d",&n);
printf("Enter Values : ");

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

printf("\%d is Largest Number\n",Largest(a,n));
printf("\%d is Smallest Number\n",smallest(a,n));

}
int Largest(int a[],int n)
{
int max;

if(n==1)
return a;

else {
max=Largest(a,n-1);

if(max>a[n-1])
return max;
else

return a[n-1];
}
}
int smallest(int a[],int n)
{
int min;

if(n==1)
return a;

else {
min=smallest(a,n-1);

if(min<a[n-1])
return min;
else

return a[n-1];
}

}
``````

Output -:

``````Enter a number : 5
Enter values
1
8
3
2
7

8 is Largest Number
1 is Smallest Number``````

## C Program To Find 2nd Largest And Smallest Number In An Array

Program -:

``````//C Program To Find the 2nd Largest and smallest Number In An Array

#include<stdio.h>
void  main()
{
int i,n,array,Largest, secLargest, smallest, secsmallest;

printf("Enter How many number you want?:\n") ;
scanf("%d",&n) ;

printf("Enter %d Values \n",n) ;
for(i=0;i<n;i++)
{
scanf("%d",&array[i]) ;
}
if (array > array) {
Largest = array;
secLargest = array;
}
else {
Largest = array;
secLargest = array;
}
for (i = 2; i < n; i++) {
if (array[i] > Largest) {
secLargest = Largest;
Largest = array[i];
}
else if (array[i] > secLargest) {
secLargest = array[i];
}
}
if (array < array) {
smallest = array;
secsmallest = array;
}
else {
smallest = array;
secsmallest = array;
}
for (i = 2; i < n; i++) {
if (array[i] < smallest) {
secsmallest = smallest;
smallest = array[i];
}
else if (array[i] < secsmallest) {
secsmallest = array[i];
}
}
printf(" \nSecond Largest Element is %d", secLargest);
printf(" \nSecond Smallest Element is %d", secsmallest);
}
``````

Output -:

``````Enter How many number you want?:
5
Enter 5 numbers
109
208
308
208
207

Second Largest Number is 208
Second Smallest Number is 207``````

## Conclusion 