Sep 17, 2013 at 2:01pm UTC
#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;
}
Sep 17, 2013 at 2:05pm UTC
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
Sep 17, 2013 at 2:31pm UTC
Did you do it by yourself?
Sep 17, 2013 at 2:47pm UTC
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
Sep 17, 2013 at 2:49pm UTC
chriscpp.......i do it with own effort
Sep 17, 2013 at 2:53pm UTC
coder777........you mean i write another int in this programm
Sep 17, 2013 at 3:00pm UTC
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 Sep 17, 2013 at 3:14pm UTC
Sep 17, 2013 at 3:09pm UTC
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 Sep 17, 2013 at 3:15pm UTC
Sep 17, 2013 at 3:35pm UTC
coder77.......which code you write also gave error.......error is that smallest and smallest2 values are same
Sep 17, 2013 at 4:06pm UTC
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?
Sep 17, 2013 at 4:25pm UTC
dukhi X.......isko solve kesay kro
Sep 17, 2013 at 4:36pm UTC
friend............plz solution bta do
below is code....what is problem in code
Sep 17, 2013 at 4:38pm UTC
#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;
}
Sep 17, 2013 at 4:44pm UTC
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 Sep 17, 2013 at 4:50pm UTC
Sep 17, 2013 at 4:55pm UTC
dost......smallest and smallest2 values same ati han
Sep 17, 2013 at 5:08pm UTC
yar......ya work nhen kerta.....6,5 errors atay han.....dukhi x