Find elements

C program to find largest and second largest elements in array

C program to find largest and second largest elements in array

In this tutorial, we will discuss the  concept of C program to find 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 C 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.

largest and second largest element in array

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

 

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

Find largest and second largest elements from integer Array

Program 1

#include <stdio.h>
#include <stdlib.h>

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

printf("Enter the number of elements in array: ");
scanf("%d",&size);//taking input from user for number of elements in array

for(i=0; i<size; i++){
    printf("Enter the array element ");
    printf("%d :",(i+1));
scanf("%d",&arr[i]);//Taking input for array elements

if(large<arr[i]){
secondLarge=large;
large=arr[i];
}
else if(secondLarge<arr[i]){
secondLarge=arr[i];
}
}
printf("\n%d is the largest nunber\n",large);
printf("%d is te second largest nunber",secondLarge);
//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 array: 6
Enter the array element 1: 67
Enter the array element 1: 89
Enter the array element 1: 78
Enter the array element 1: 54
Enter the array element 1: 23
Enter the array element 1: 42

89 is the largest number
78 is the second largest number

 

Find largest and second largest elements from float Array

Program 2

#include <stdio.h>
#include <stdlib.h>

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

printf("Enter the number of element in te array: ");
scanf("%d",&size);//taking input from user for no of elements in array

for(i=0; i<size; i++){
    printf("Enter the array element ");
    printf("%d: ",(i+1));
scanf("%f",&arr[i]);//taking input fron user to array elements

if(large<arr[i]){
secondLarge=large;
large=arr[i];
}
else if(secondLarge<arr[i]){
secondLarge=arr[i];
}
}
printf("\n%.2f is the largest nunber\n",large);
printf("%.2f is the second largest nunber",secondLarge);
//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: 45.3
Enter the array element 2: 89.67
Enter the array element 1: 12.45
Enter the array element 1: 76.32
Enter the array element 1: 45.67
Enter the array element 1: 65.32

89.67 is the largest number
76.32 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 java

Cpp program to largest and second largest elements in array
Python program to find largest number among three numbers
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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

1 month ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago