help required friends

Pages: 12
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n,smallest;
cout << "Enter A Number :" << endl;
cin >> n;
if (n >= 0)
smallest=n;
while (n >= 0)
{
cout << "Enter A Number :" << endl;
cin >> n;
if (n >= 0 && n < smallest)
smallest=n;
}
cout << "Smallest = " << smallest << endl;
getch();
return 0;
}
this program for smallest number and i also want to find 2nd smallest ...for this what will be the condition.....plz plz plz help this time
Did you do it by yourself?
you need another variable like n let's say n1. the smallest goes still to n. if it's greater than n but smaller than n1 it goes to n1
chriscpp.......i do it with own effort
coder777........you mean i write another int in this programm


















Yes
i also add second variable but when i run it he telled about only smallest
and at second smallest he give ans zero the number which i inialized the variable
Last edited on
Ok, sorry. I see that I confused you.
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
#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;
} 
[EDIT]forgot to initialize smallest2
Last edited on
coder77.......which code you write also gave error.......error is that smallest and smallest2 values are same
coder77....plz reply
Sounds like your trying to:

- Get two numbers
- Find out which one is smaller
- Report the smaller and logically the bigger one.

Is this correct?
yes.....correct
whenever u'll enter smaller number than previously stored in smallest that means previous value in smallest is 2nd smallest number.

1
2
3
4
5
6
7
8
if(n < smallest)
{
smallest2=smallest;
smallest=n;
}
else if(n < smallest2))
smallest2 = n;
}
dukhi X.......isko solve kesay kro
friend............plz solution bta do
below is code....what is problem in code
#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;
}
delete line 17 to line 20 from coder777s code and write these lines there.

1
2
3
4
5
6
7
8
if(n < smallest)
{
smallest2=smallest;
smallest=n;
}
else if(n < smallest2))
smallest2 = n;
}


syntax error in you code:

else if(n < smallest2)) // you are using )) 2 parantheses to close.error
Last edited on
dost......smallest and smallest2 values same ati han
yar......ya work nhen kerta.....6,5 errors atay han.....dukhi x
Pages: 12