#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n,smallest,smallest2;
cout << "Enter A Number :" << endl;
cin >> n;
smallest=n;
smallest2=n; // ok
while (n >= 0)
{
cout << "Enter A Number :" << endl;
cin >> n;
if (n >= 0)
{
if(n < smallest)
smallest=n;
else if(n < smallest2))
smallest2 = n;
}
}
cout << "Smallest = " << smallest << endl;
cout << "2nd Smallest = " << smallest2 << endl;
getch();
return 0;
}
when i put value then smallest and second smallest values are same
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
int main()
{
int n,smallest,smallest2;
cout << "Enter A Number :" << endl; //lets pick 5
cin >> n; //n=5
smallest=n; //smallest=5
smallest2=n; // smallest2=5
while (n >= 0) //yes y>0
{
cout << "Enter A Number :" << endl;
cin >> n; //lets pick 4
if (n >= 0)//yes 4>0
{
if(n < smallest)//yes 4<5
{
smallest=n; //smallest = 4, missed a closing brace here
}//fixed with a brace
else if(n < smallest2)
{
smallest2 = n;
}
}//if n>0
}// while
cout << "Smallest = " << smallest << endl;
cout << "2nd Smallest = " << smallest2 << endl;
getch();
return 0;
}
|
See what you get with this now.
Last edited on