Find elements

Cpp program to largest and second largest elements in array

Cpp program to largest and second largest elements in array

In this tutorial, we will discuss the concept of Cpp program to display largest and second largest elements in Array.

In this topic, we will learn how to find the largest and second largest elements of array elements in Cpp programming language

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 largest and second largest elements from the array using for loop and if statements.

the largest and second largest number in an array

In this programs, we can see step by step procedure for completion of the program.

  • array declaration
  • Essential variable declaration
  • Get input from the user for array length
  • Get input from the user for array elements
  • loop for find largest and second largest value
  • Display result on the screen

Find largest and second largest elements from integer Array

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int size,i;
    int arr[50];//variable declaration to size of array
int large=0, secondLarge=0
;

cout<<"Enter the number of element in array: ";
cin>>size;//taking input from user for no of elements in array

for(i=0; i<size; i++){
    cout<<"Enter the array element"<<i+1<<": ";
cin>>arr[i];//taking input from user for array elements

if(large<arr[i]){
secondLarge=large;
large=arr[i];
}
else if(secondLarge<arr[i]){
secondLarge=arr[i];
}
}
cout<<"\n";
cout<<""<<large<<" is a largest nunber\n";
cout<<secondLarge<<" is a second largest nunber";
//display result on the screen
getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter the number of elements in the array: 6 
Enter the array element 1: 34
Enter the array element 2: 67 
Enter the array element 3: 89
Enter the array element 4: 98
Enter the array element 5: 32

98 is the largest number 
89 is the second largest number

 

Find largest and second largest elements from float Array

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int size,i;
    float arr[50];//variable declaration for size of array
float large=0.0, secondLarge=0.0;

cout<<"Enter the number of element in the array: ";
cin>>size;//taking input from user for no of elements

for(i=0; i<size; i++){
    cout<<"Enter the array element"<<i+1<<": ";
cin>>arr[i];//taking input from user to array elements

if(large<arr[i]){
secondLarge=large;
large=arr[i];
}
else if(secondLarge<arr[i]){
secondLarge=arr[i];
}
}
cout<<"\n";
cout<<""<<large<<" is the largest nunber\n";
cout<<secondLarge<<" is the second largest nunber";
//display result on the screen
getch();
    return 0;
}

When the above code is executed, it produces the following results

Enter the number of elements in the array: 6
Enter the array element 1: 89.7
Enter the array element 2: 45.3
Enter the array element 3: 12.56
Enter the array element 4: 54.32
Enter the array element 5: 76.43

89.7 is the largest number
76.43 is the second largest number

 

Suggested for you

Single dimension array in C++

Operator in C++ language

For loop in C++ language

If statements of C++ language

Java code to largest and second largest elements in array
C program to find largest and second largest elements in array
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

1 month ago

Java program to check odd or even using recursion

Java program to check odd or even using recursion In this tutorial, we discuss a…

1 month ago