Pages

Sunday, November 09, 2014

Write a program that print the 2nd smallest number 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 , min = 0, min2 = 0;
    printf("Enter the 5 integer No to find 2nd smallest No.: ");
    for(i=0; i<5; i++)
        scanf("%d",&arr[i]);
    min=arr[0];
    for(i = 1; i < 5; i++) {
        if(min > arr[i]) {
            min = arr[i];
            save = i;
        }
    }// for loop
    min2 = arr[0];
    for(i = 1; i < 5; i++) {
        if(min2 > arr[i] && save != i)
            min2 = arr[i];
    }// for loop
    printf("\n2nd smallest no. is %d\n\n\n", min2);
}// main() ... the end

OutPut:

Enter the 5 integer No. to find 2nd smallest No.: 45
54
65
76
6

2nd smallest no. is 45 

No comments:

Post a Comment