Answer:
#include <stdio.h>
int main(void) {
int num1;
int num2;
int num3;
printf("Enter three integers: ");
scanf("%d", &num1);
scanf("%d", &num2);
scanf("%d", &num3);
if (num1 == 0 || num2 == 0 || num3 == 0)
{
printf("please input a number greater than zero :)\n");
}
if (num1 <= num2 && num1 <= num3)
{
printf("%i is the smallest number!\n", num1);
}
else if (num2 <= num1 && num2 <= num3)
{
printf("%i is the smallest number!\n", num2);
}
else
{
printf("%i is the smallest number!\n", num3);
}
return 0;
}
Explanation:
Alright so let's start with the requirements of the question:
- must take 3 integers from user input
- determine which of these 3 numbers are the smallest
- spit out the number to out
So we needed to create 3 variables to hold each integer that was going to be passed into our script.
By using scanf("%i", &variableName) we were able to take in user input and store it inside of num1, num2, and num3.
Since you mentioned you were new to the C programming language, I threw in the first if statement as an example of how they can be used, use it as a guide for future reference, sometimes it's better to understand your code visually.
Basically what this if statement does is, it checks to see if any of the integers that came in from user input was the number zero, it told the user it does not accept that number, please input a number greater than zero.
if (num1 == 0 || num2 == 0 || num3 == 0)
{
printf("please input a number greater than zero :)\n");
}
I used this methodology and implemented the heart of the question,
whichever number is smaller, print it out on the shell (output).
if (num1 <= num2 && num1 <= num3)
^^ here we're checking if the first variable we created is smaller than the second variable and the third ^^
{
printf("%i is the smallest number!\n", num1);
^^ if it is smaller, then print integer and then print a new line so the next line looks neat ^^
( incase if your wondering what "\n" is, its a special character that allows you so print a new line on the terminal, kind of like hitting the return or enter key )
}
else if (num2 <= num1 && num2 <= num3)
^^ else if is used when your checking for more than one thing, and so for the second variable we checked to see if it was smaller than the first and third variable we created ^^
{
printf("%i is the smallest number!\n", num2); < -- and we print if it's smaller
}
Last but not least:
else
^^ if it isn't num1 or num2, then it must be num3 ^^
{
printf("%i is the smallest number!\n", num3);
we checked the first two options, if its neither of those then we have only one variable left, and thats num3.
}
I hope that helps !!
Good luck on your coding journey :)