Solution in C language

the question is…
You are given a sequence of integers as input, terminated by a -1. (That is, the input integers may be positive, negative or 0. A -1 in the input signals the end of the input.)

-1 is not considered as part of the input.

Find the second largest number in the input. You may not use arrays.

I am a beginner, so please don’t laugh on me…

use int i,j,k; i= initial input1 , j= 2nd input if i>j swap or else proceed; use while(&input!=-1) next input value = k ; if(k>i) j=i,i=k ; if not proceed; after while loop print j;

1 Like

Refer to geeksforgeeks post for this. Beginners like us are required to go through such sites i believe because they help develop our thinking in the right direction. Just google “how to find second smallest number” and u’ll get to see lot of links.

#include “stdio.h”

int FindSecondLargestFromIntSeries() {

int n = 0, big = 0, big2 = 0, temp = 0;
bool bOnlyOnce = true; // This is just for 2nd largest no if on 2nd input ie, if 2nd input is < big we have to consider that as the 2nd largest no.

printf("Enter the number\n");
printf("Enter -1 to end\n");
scanf("%d", &n);
big = big2 = n;



while (n != -1) {

	scanf("%d", &n);

	if( n == -1)
		break;

	if(n > big) {
		temp = big;
		big = n;
		if ( n > big2) {
			big2 = temp;
			bOnlyOnce = false;
		}
	} 
	else if ((big2 < n) || bOnlyOnce) // bOnlyOnce - This is just for 2nd largest no if on 2nd input
	{
			big2 = n;
			bOnlyOnce = false; 
	}

}
if(big2 != -1)
    printf("Second Largest no is: %d", big2);
else
	printf("Second Largest no is: None");

return big2;

}

int _tmain(int argc, _TCHAR* argv[])
{
FindSecondLargestFromIntSeries();
return 0;
}

1 Like

This is how you do it …

Notice that there must be atleast 2 numbers to decide second_largest.

Step 1. input 2 numbers . let one be largest , other be second_largest. Rest of numbers we will input in a while loop.

Step 2.It might be possible that the first 2 numbers be 3, 5 . in such a case second_largest becomes greater than largest . hence if such a condition arise , swap their values. The code till here is :

int main()
{
        int largest,second_largest ,num;

        scanf("%d",&largest);
        scanf("%d",&second_largest);

        if(largest<second_largest)
        {
//i am swapping values "if" such condition arise , "num" is just a temporary variable
                num=largest;
                largest=second_largest;
                second_largest=num;

        }

        while(1)
        {
            //some logic that i will explain later


        }

        printf("%d\n",second_largest);

        return 0;
}

Step 3: Now i will input all values in while loop. All values i input save in “num” variable.As soon as i encounter -1 , i break from loop and present answer.

Step 4:I am taking a loop that will always run infinitely . hence i take “while(1)”. such a loop always run till infinity. now inside , i scanf and input value in “num” variable . if value is -1 , i break from loop. Else , i check if value is greater than largest. if so second_largest becomes largest and largest becomes num. Ex : suppose , largest was 5 and second largest was 4 . i input another value say ,6 . now among 5,4,6 , largest =6 and second_largest =5 (and not 4).Update your second largest also.

Step 5: In case our num is between largest and second_largest , then only our second largest = num. Largest remains unchanged.I have used else if in the code to achieve this. else if is reached only when the above if statement fails. that means largest<= num < second_largest .

Here’s the full code :

int main()
{
        int largest,second_largest ,num;

        scanf("%d",&largest);
        scanf("%d",&second_largest);

        if(largest<second_largest)
        {
                num=largest;
                largest=second_largest;
                second_largest=num;

        }

        while(1)
        {
            scanf("%d",&num);

            if(num==-1)
                break;

            if(num>largest)
            {
                    second_largest=largest;
                    largest=num;
            }
            else if(num>second_largest) 
                second_largest=num;


        }

        printf("%d\n",second_largest);

        return 0;
}
1 Like
//