Pages

Sunday, November 09, 2014

Write a program that print the 2nd largest from 5 numbers by using array

INPUT:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include<stdio.h>
int main()
{
    int arr[5], i , save = 0 , lar = 0, lar2 = 0;
    printf("Enter the 5 integer value to find 2nd largest value: ");
    for(i=0; i<5; i++)
        scanf("%d",&arr[i]);
    lar=arr[0];
    for(i = 1; i < 5; i++) {
        if(lar<arr[i]) {
            lar = arr[i];
            save = i;
        }
    }// for loop
    lar2 = arr[1-1];
    for(i = 1; i < 5; i++) {
        if(lar2 < arr[i] && save != i)
            lar2 = arr[i];
    }// for loop
    printf("\n2nd largest no. is %d\n\n\n", lar2);
}// main() ... the end

OutPut:

Enter the 5 integer value to find 2nd largest value: 45
54
65
76
6

2nd largest no. is 65 


No comments:

Post a Comment