11. Program that inputs the number of students in the class. It then inputs the marks of these students and displays the highest and second highest marks.
Chapter 6:
Looping Structures
Programming Exercise
Problem # 11:
Write a program that inputs the number of students in the class. It then inputs the marks of these students and displays the highest and second highest marks.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int s, count=2, highest, marks, highest_2;
cout<<"Enter number of students:";
cin>>s;
cout<<endl<<"Enter marks:";
cin>>marks;
highest_2=highest=marks;
cout<<endl<<"Enter marks:";
cin>>marks;
if(marks>highest_2)
highest=marks;
else
highest_2=marks;
while(count<s)
{
cout<<endl<<"Enter marks:";
cin>>marks;
if((marks>highest_2)&&(marks<highest))
highest_2=marks;
if(marks>highest_2)
highest=marks;
count++;
}
cout<<endl<<"Highest marks are:"<<highest;
cout<<endl<<"2nd highest marks are:"<<highest_2;
return 0;
}
#include<conio.h>
using namespace std;
int main()
{
int s, count=2, highest, marks, highest_2;
cout<<"Enter number of students:";
cin>>s;
cout<<endl<<"Enter marks:";
cin>>marks;
highest_2=highest=marks;
cout<<endl<<"Enter marks:";
cin>>marks;
if(marks>highest_2)
highest=marks;
else
highest_2=marks;
while(count<s)
{
cout<<endl<<"Enter marks:";
cin>>marks;
if((marks>highest_2)&&(marks<highest))
highest_2=marks;
if(marks>highest_2)
highest=marks;
count++;
}
cout<<endl<<"Highest marks are:"<<highest;
cout<<endl<<"2nd highest marks are:"<<highest_2;
return 0;
}
Let me know in the comment section if you have any question.
Previous Post:
10. Program that inputs a number from the user and displays all perfect numbers up to the number entered.
if((marks>highest_2)&&(marks<highest))
ReplyDeleteif we put
if((marks==highest_2)&&(marks<highest))
than second highest marks =1
what is the reason