PDA

View Full Version : C program



fernandoBOT
03-12-2008, 09:57 PM
so i'm supposed to find the smallest and largest numbers from 3 numbers a user inputs.

i have this so far:




#include<stdio.h>

int main(void)

{

int num1;
int num2;
int num3;

printf("Enter a number: ");
scanf("%d", &num1);

printf("Enter a number: ");
scanf("%d", &num2);

printf("Enter a number: ");
scanf("%d", &num3);

if (num1 > 0 && num2 > 0 && num3 > 0)

else if(num1 < num2 && num1 < num3)
printf("The smallest number entered was %d \n", num1);

else if (num2 < num1 && num2 < num3)
printf("The smallest number entered was %d \n", num2);

else
printf("The smallest number entered was %d \n", num3);

if (num1 > 0 && num2 > 0 && num3 > 0)

else if(num1 > 0 && num1 > num2 && num1 > num3)
printf("The Largest number entered was %d \n", num1);

else if (num2 > 0 && num2 < num1 && num2 < num3)
printf("The Largest number entered was %d \n", num2);

else {printf("The largest number entered was %d \n", num3);}

return(0);

}



unfortunately that's as far as i'm getting.

help?

ryph
03-12-2008, 10:02 PM
//largest
if ( (num1 > num2) && (num1 > num3) ){
printf("&#37;d is the largest.\n", num1);
}else if ( (num2 > num1) && (num2 > num3) ){
printf("%d is the largest.\n", num2);
}else{
printf("%d is the largest.\n", num3);
}

//smallest
if ( (num1 < num2) && (num1 < num3) ){
printf("%d is the smallest.\n", num1);
}else if ( (num2 < num1) && (num2 < num3) ){
printf("%d is the smallest.\n", num2);
}else{
printf("%d is the smallest.\n", num3);
}

mhu
03-12-2008, 10:06 PM
is one of the input stipulations to not have to enter any number less than zero?

otherwise i don't really see why there's an if statement for > 0

fernandoBOT
03-12-2008, 10:10 PM
yeah, it's only suppose to display numbers greater than 0.

fernandoBOT
03-12-2008, 10:12 PM
//largest
if ( (num1 > num2) && (num1 > num3) ){
printf("%d is the largest.\n", num1);
}else if ( (num2 > num1) && (num2 > num3) ){
printf("%d is the largest.\n", num2);
}else{
printf("%d is the largest.\n", num3);
}

//smallest
if ( (num1 < num2) && (num1 < num3) ){
printf("%d is the smallest.\n", num1);
}else if ( (num2 < num1) && (num2 < num3) ){
printf("%d is the smallest.\n", num2);
}else{
printf("%d is the smallest.\n", num3);
}


thank you.

fernandoBOT
03-12-2008, 10:21 PM
question: if i were to want to expand it so that it checks 7 different numbers would it be best to do some type of loop?

ryph
03-12-2008, 10:26 PM
yea, make an array of numbers and loop through them

going with your 7 number q


//this will tell you highest num from 7
//i assume the arrayofnums already has been inputted
int arrayofnums[7], currenthigh=0;

for (int a=0; a<7; a++){
if (arrayofnums[a]>currenthigh)
currenthigh=arrayofnums[a];
}
printf("highest number=&#37;d\n", currenthigh);