16. Program that continuously inputs positive integer values from the user. The program should finally display the second largest number entered.
Chapter 6:
Looping Structures
Programming Exercise
Problem # 16:
Write a program that continuously inputs positive integer values from the user. The user enters a zero to show that he has no more values to enter. The program should finally display the second largest number entered.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int highest, marks, highest_2;
cout<<"Enter a positive number (0 for exit): ";
cin>>marks;
if(marks>0)
{
highest_2=highest=marks;
do{
if(marks>highest)
{
highest_2=highest;
highest=marks;
}
cout<<endl<<"Enter a positive number (0 for exit): ";
cin>>marks;
}
while(marks>0);
cout<<endl<<"2nd highest number is:"<<highest_2;
}
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
int highest, marks, highest_2;
cout<<"Enter a positive number (0 for exit): ";
cin>>marks;
if(marks>0)
{
highest_2=highest=marks;
do{
if(marks>highest)
{
highest_2=highest;
highest=marks;
}
cout<<endl<<"Enter a positive number (0 for exit): ";
cin>>marks;
}
while(marks>0);
cout<<endl<<"2nd highest number is:"<<highest_2;
}
return 0;
}
Let me know in the comment section if you have any question.
Previous Post:
15. Program that inputs an integer and displays whether it is a prime number or not.
Comments
Post a Comment