Question

Write a C++ program that reads a list of numbers 10 and displays largest, second largest...

Write a C++ program that reads a list of numbers 10 and displays largest,
second largest and third largest.

Note: Please do it with loops don't used arrays

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include<iostream>
using namespace std;
int main()
{
        int i,f=0,s=0,m=0,num,temp,temp1;
        for(i=0;i<10;i++)
        {
                cout<<"enter the " <<i+1<<" number\n";
                cin>>num;
                if(i==0)
                        {
                        f=num;  
                        }
                if(i==1)
                        {
                        if(f<num)
                        {       
                                temp=f;
                                f=num;
                                s=temp;
                        }
                        else if(f==num)
                        {
                                continue;       
                        }
                        else
                        {
                                s=num;
                        }
                        
                        }                       
                else
                {
                        if(f==num||s==num)
                        {
                                continue;
                        }
                        if(f<num)
                                {
                                temp=f;
                                f=num;
                                temp1=s;
                                s=temp;
                                m=temp1;
                                }
                        else if(s<num)
                        {
                                temp=s;
                                s=num;
                                m=temp;                 
                        }       
                        else if(num>m)
                                {
                                        m=num;
                                }
                        else
                         continue;              
                }
        }
        if(s==0)
        {
        cout<<"1st:"<<f<<" \t Not yet found 2nd largest elemnt \t Not yet found 3rd largest elemnt";  
                
        }
        if(s!=0 && m==0)
        {
        cout<<"1st:"<<f<<"\t2nd:"<<s<<"\t Not yet found 3rd largest elemnt";
                        
        }
        else
        {
        cout<<"1st largest:"<<f<<"\t2nd largest:"<<s<<"\t3rdlargest:"<<m;
        return 0;
        }
}

output::

Add a comment
Answer #2

Here's a C++ program that reads a list of 10 numbers and displays the largest, second largest, and third largest numbers using loops:

cppCopy code#include <iostream>int main() {    int largest = INT_MIN;    int secondLargest = INT_MIN;    int thirdLargest = INT_MIN;    for (int i = 1; i <= 10; i++) {        int num;
        std::cout << "Enter number " << i << ": ";
        std::cin >> num;        if (num > largest) {
            thirdLargest = secondLargest;
            secondLargest = largest;
            largest = num;
        } else if (num > secondLargest) {
            thirdLargest = secondLargest;
            secondLargest = num;
        } else if (num > thirdLargest) {
            thirdLargest = num;
        }
    }

    std::cout << "Largest: " << largest << std::endl;
    std::cout << "Second Largest: " << secondLargest << std::endl;
    std::cout << "Third Largest: " << thirdLargest << std::endl;    return 0;
}

In this program, we initialize three variables (largest, secondLargest, and thirdLargest) with the minimum possible integer value (INT_MIN). We then use a loop to read 10 numbers from the user. For each number, we compare it with the current largest, second largest, and third largest numbers and update the variables accordingly.

Finally, we display the largest, second largest, and third largest numbers obtained from the loop.


answered by: Mayre Yıldırım
Add a comment
Know the answer?
Add Answer to:
Write a C++ program that reads a list of numbers 10 and displays largest, second largest...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT