Problem statement:- Program to Find the sum of N numbers in an array.
Data requirement:-
Input Data:- arr,size
Output Data:-sec_max
Output Data:-sec_max
Additional Data:- i, j, max
Program in C
Here is the source code of the C Program to Find the 2nd largest element in the array.
Code:
#include<stdio.h>
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],i=0,max=INT_MIN,sec_max=INT_MAX,j=0;
printf("Enter the Element of the array:\n");
while(j<size)
{
scanf("%d",&arr[j]);
j++;
}
while(i<size)
{
if(arr[i]>=max)
{
sec_max=max;
max=arr[i];
}
else if(arr[i]>=sec_max)
sec_max=arr[i];
i++;
}
printf("The 2nd largest element of array: %d",sec_max);
}
#include<limits.h>
int main()
{
printf("Enter the size of the array:");
int size;
scanf("%d",&size);
int arr[size],i=0,max=INT_MIN,sec_max=INT_MAX,j=0;
printf("Enter the Element of the array:\n");
while(j<size)
{
scanf("%d",&arr[j]);
j++;
}
while(i<size)
{
if(arr[i]>=max)
{
sec_max=max;
max=arr[i];
}
else if(arr[i]>=sec_max)
sec_max=arr[i];
i++;
}
printf("The 2nd largest element of array: %d",sec_max);
}
Input/Output:
Enter the size of the array:4
Enter the Element of the array:
20
30
45
78
The largest element of array: 45
Enter the Element of the array:
20
30
45
78
The largest element of array: 45
Program in C++
Here is the source code of the C++ Program to Find the 2nd largest element in the array.
Code:
#include<iostream>
#include<climits>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],i=0,max=INT_MIN,sec_max=INT_MAX,j=0;
cout<<"Enter the Element of the array:\n";
while(j<size)
{
cin>>arr[j];
j++;
}
while(i<size)
{
if(arr[i]>=max)
{
sec_max=max;
max=arr[i];
}
else if(arr[i]>=sec_max)
sec_max=arr[i];
i++;
}
cout<<"The 2nd largest element of array: "<<sec_max;
}
#include<climits>
using namespace std;
int main()
{
cout<<"Enter the size of the array:";
int size;
cin>>size;
int arr[size],i=0,max=INT_MIN,sec_max=INT_MAX,j=0;
cout<<"Enter the Element of the array:\n";
while(j<size)
{
cin>>arr[j];
j++;
}
while(i<size)
{
if(arr[i]>=max)
{
sec_max=max;
max=arr[i];
}
else if(arr[i]>=sec_max)
sec_max=arr[i];
i++;
}
cout<<"The 2nd largest element of array: "<<sec_max;
}
Input/Output:
Enter the size of the array:5
Enter the Element of the array:
30
45
20
89
99
The 2nd largest element of array: 89
Enter the Element of the array:
30
45
20
89
99
The 2nd largest element of array: 89
Program in Java
Here is the source code of the Java Program to Find the 2nd largest element in the array.
Code:
import java.util.Scanner;
public class p10 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i=0,max=Integer.MIN_VALUE,sec_max=Integer.MIN_VALUE,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
while(i<size)
{
if(arr[i]>=max)
{
sec_max=max;
max=arr[i];
}
else if(arr[i]>=sec_max)
sec_max=arr[i];
i++;
}
System.out.println("The 2nd largest element of array: "+sec_max);
sc.close();
}
}
public class p10 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size of the array:");
int size;
size=sc.nextInt();
int arr[ ]=new int[size];
int i=0,max=Integer.MIN_VALUE,sec_max=Integer.MIN_VALUE,j=0;
System.out.println("Enter the Element of the array:");
while(j<size)
{
arr[j]=sc.nextInt();
j++;
}
while(i<size)
{
if(arr[i]>=max)
{
sec_max=max;
max=arr[i];
}
else if(arr[i]>=sec_max)
sec_max=arr[i];
i++;
}
System.out.println("The 2nd largest element of array: "+sec_max);
sc.close();
}
}
Input/Output:
Enter the size of the array:
4
Enter the Element of the array:
15
20
45
35
The 2nd largest element of array: 35
4
Enter the Element of the array:
15
20
45
35
The 2nd largest element of array: 35
Program in Python
Here is the source code of the Python Program to Find the 2nd largest element in the array.
Code:
import sys
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
max=-sys.maxsize-1
sec_max=-sys.maxsize-1
for j in range(0,size):
if (arr[j] >= max):
sec_max=max
max = arr[j]
elif(arr[i] >= sec_max):
sec_max = arr[j]
print("The 2nd largest element of array: ",sec_max)
arr=[]
size = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,size):
num = int(input())
arr.append(num)
max=-sys.maxsize-1
sec_max=-sys.maxsize-1
for j in range(0,size):
if (arr[j] >= max):
sec_max=max
max = arr[j]
elif(arr[i] >= sec_max):
sec_max = arr[j]
print("The 2nd largest element of array: ",sec_max)
Input/Output:
Enter the size of the array: 4
Enter the Element of the array:
29
48
78
200
The 2nd largest element of array: 78
Enter the Element of the array:
29
48
78
200
The 2nd largest element of array: 78
2 Comments
ReplyDelete//9. Write a program to find the 2nd largest element in the array
import java.util.*;
import java.util.Scanner;
class array91
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("enter the number of elements in array:");
int n = s.nextInt();
int a[] = new int[n];
System.out.println("enter the elements of array");
for(int i = 0 ; imax)
{
max=a[i];
}
}
System.out.println("largest number is" +max);
int maxb =0;
System.out.println("sorted array is :" );
for(int i=0;i<n;i++)
{
Arrays.sort(a);
System.out.println(a[i]);
}
System.out.println("second largest no. is :" );
System.out.println(a[n-2]);
}
}
Thank u so much
ReplyDeletePlease do not Enter any spam link in the comment box