Second Largest Number in Array
Welcome to this tutorial where you will learn how to find the second largest number in an array using the C programming language.
Arrays are a collection of similar data types, and they are widely used in programming. In many programming tasks, finding the second largest number in an array is an essential requirement, and being able to perform this operation efficiently is a valuable skill.
In this tutorial, we will provide you with a comprehensive guide to finding the second largest number in an array using the C programming language. We will cover different methods to perform this operation, including using loops and functions.
You will learn how to write a C program to find the second largest number in an array, and we will also provide you with clear explanations and easy-to-understand examples.
The process of finding the second largest number in an array is slightly more complex than finding the largest or smallest number, but with the methods covered in this tutorial, you will be able to perform this operation with ease.
Example:
We have an array a[10]={3,44,32,11,4,5,66,54,60,50}
So, 60 is the largest number in this array.
Source code:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[10],i,larg,pos;
printf("Enter the number of array: ");
for(i=0;i<10;i++){
scanf("%d",&a[i]);
}
printf("The largest number of arrays is: ");
larg=a[0];
pos = 0;
for(i=0;i<10;i++){
if(larg>a[i]){
larg=larg;
}
else{
larg=a[i];
pos=i;
}
}
printf("%d",larg);
printf("\n position of first largest element is %d",pos);
for(i=pos;i<9;i++){
a[0]=a[i+1];
}
printf("\n Print the element of array\n");
for(i=0;i<9;i++){
printf("\n%d",a[i]);
}
printf("The Second largest number of arrays is: ");
larg=a[0];
for(i=1;i<9;i++){
if(larg>a[i]){
larg=larg;
}
else{
larg=a[i];
}
}
printf("%d",larg);
return 0;
}
Output:
Enter the number of array:
5
34
55
443
22
33
555
44
3
51
The largest number of arrays is: 555
Position of first largest element is 3
Print the element of array:
5
34
55
443
22
33
44
3
51
The Second largest number of arrays is: 443
0 Comments