Help w/ little program.

Hello everyone, I have a problem with my formula. I am trying to display the smallest of 3 numbers. It works ok when I do something like 1 2 and 3 but when I do 4 3 1 and outputs double.

Please help.

#include <iostream>

using namespace std;

int main () {

int num1, num2, num3, minimum;

cout << "Enter 3 integers with a space between them: ";

cin >> num1 >> num2 >> num3;

if ( num1 <= num2 || num1 <= num3)

cout << "The smallest value among " << num1

<< ", " << num2 << ", and " << num3 << " is " << num1 << endl;

else if ( num2 <= num1 || num2 <= num3 )

cout << "The smallest value among " << num1

<< ", " << num2 << ", and " << num3 << " is " << num2 << endl;

if ( num3 <= num1 || num3 <= num2 )

cout << "The smallest value among " << num1

<< ", " << num2 << ", and " << num3 << " is " << num3 << endl;

return 0;

}
Change

if ( num1 <= num2 || num1 <= num3)


to

if ( num1 <= num2 && num1 <= num3)

EDIT: Also it is not clear why variable minimum was defined.
Last edited on
Vlad you are a work animal. :)

Thank you.
you can do the same thing with less rows.
#include<iostream.h>
int main()
{
int a,b,c,min;
cout<<"Enter 3 numbers and program will show the smallest number"<<endl;
cin>>a>>b>>c;
min=a;
if (min>b) min=b;
if (min>c) min=c;
cout<<"The smallest value among "<<min<<endl;
return 0;
}
Topic archived. No new replies allowed.