## Find second largest number problem solving

In this above problem we have to determine the second largest number inside the given three numbers of user. We will take three integers as input and check by our program that which number is the second largest among them. Then we will print that number.

We have taken this problem from CodeChef for practice and learning purpose only. You should bear in mind that the solution we have given here is not the only solution to this problem. Rather it will be better if you find the solution by your own.

We have to compare each number with rest two and then we will take the decision that which number is the second largest. Let’s see a solution of this problem.

## Solution of finding second largest problem

Here in the given solution we have taken a variable T to store the test case and then we have used a for loop for rotating the program T times. For each test case we will take three number and then compare them.

If the first number is greater than second and less than third or greater than third and less than first number, then we will print that the first number is the second largest. We will check this for second number also. If first and second number is not the second largest then the third number will automatically be the second largest number.

Assume a user gives three number 5, 10 and 15. Now our program will check the second number first. Is second number is less than first number and greater than third number? The answer is ‘Yes’. And then the program will print the second number as the second largest number.

```
// solution of second largest number in C
#include <stdio.h>
int main(){
int T, i;
scanf("%d", &T);
for(i = 1; i <= T; i++){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
if((a >= b && b >= c) || (c >= b && b >= a)){
printf("%d\n", b); // b is the second largest
}else if((b >= a && a >= c) || (c >= a && a >= b)){
printf("%d\n", a); // a is the second largest
}else{
printf("%d\n", c); // c is the second largest
}
}
return 0;
}
```

### Output of second largest program

When you compile and run the above program you will see the following output. You can also check the program for different input.

```
3
120 11 400
120
10213 312 10
312
10 3 450
10
```

First line contains 3 which is the number of test case. Then the given three number is 120, 11 and 400. As 120 is the second largest among them, the program prints 120. Similarly, the program have given the other output also.

However, if you have any other logic to solve this problem then you should try to implement that. This is not the only solution of this second largest number program.

**Previous page**:Â **Reverse the number**

**Next page**: Â **Valid triangle**

**Recommended for you**: