CurledMark

Second largest number program in C

Contents

  1. Introduction
  2. Algorithm
  3. Explanation of the logic
  4. Program
  5. Output
  6. Conclusion

Introduction of the second largest number

Finding the second largest value in an array is a classic C array program.

This program gives you an insight into the iteration, and array. Also, conditional operators.

We iteratively check each element to determine the largest and second largest element.

Algorithm of second largest number program

Explanation of the logic

Consider the Smallest number as the second largest. Do a comparison of all the numbers one by one in an array.

The number which is greater than the Second largest number, should not be greater than the largest number.

Then we got the second largest number.

In the program, the first while loop has been dedicated to accepting the numbers from the keyboard and the second while loop is for finding the largest and smallest number so the if loop compares the smallest and the largest number.

If they are equal, the second largest number cannot be determined. In the else part, we have compared the numbers in the array with the second largest.

If it is greater than this and not the same as the largest number then that is the second largest number.

Finally, we have printed it and this is how the implementation works in this program.

Accept 10 numbers in an array and determine the second largest number.

Note: User may feed the same number 10 times. In this case, print all are the same.

#include<stdio.h>
int main()
{
int x[10],y,largest,smallest,secondLargest;
y=0;
while(y<=9)
{
printf(“Enter a number:”);
scanf(“%d”,&x[y]);
y++;
}
largest=x[0];
smallest=x[0];
y=1;
while(y<=9)
{
if(x[y]>largest)
{
largest=x[y];
}
if(x[y]<smallest)
{
smallest=x[y];
}
y++;
}
if(smallest == largest)
{
printf("All are same, second largest cannot be determined");

}
else
{
secondLargest=smallest;
}
y=0;
while(y<=9)
{
if(x[y]>secondLargest && x[y]!=largest)
{
secondLargest=x[y];
}
y++;
}
printf("Second largest: %d\n",secondLargest);
return 0;
}

Output

Enter a number: 12

Enter a number: -1

Enter a number: 36

Enter a number: 54

Enter a number: 7

Enter a number: 82

Enter a number: 62

Enter a number: 95

Enter a number: 80

Enter a number: 5

Second Largest: 82

Reference

http://www.freebookcentre.net/Language/Free-C-Books-Download.html

http://www.freebookcentre.net/programming-books-download/Computer-Programming-Fundamentals-Using-C-Part1.html

Conclusion

So, here is all about the second largest number in the array in C. We have tried our best to explain the logic and program so we hope you find this implementation alternative. Stay tuned for the upcoming blogs.

Also, read

https://curledmark.in/the-largest-number-program-in-an-array-in-c-programming/

https://curledmark.in/armstrong-number-and-arrays-in-c-programming/

https://curledmark.in/prime-reversepower-programs-in-c-programming/

The Book Of Five Rings Summary

The Blue Umbrella

How to Talk to Anyone by Leil Lowndes

Jonathan Livingston Seagull Book Summary

Your Brain on PORN Book Summary by Gary Wilson

THE IMMORTALS OF MELUHA By Amish Tripathi full summary

Eleven Minutes by Paulo Coelho Summary

Hyperfocus by Chris Bailey Summary

Black Beauty by Anna Sewell Summary

You Can Heal Your Life by Louise Hay Summary

ATTITUDE IS EVERYTHING BY JEFF KELLER SUMMARY

THE BIOGRAPHY OF SWAMI VIVEKANANDA

THE SHAWSHANK REDEMPTION EXPLAINED

Turtles All the Way Down by John Green Story Explained