[英]program to find the largest and smallest among three entered numbers and also display whether the identified largest/smallest number is even or odd
This is my homework and i am stuck with how should i identify that the smallest/largest number is even or odd.这是我的作业,我对如何识别最小/最大数字是偶数还是奇数感到困惑。
#include <stdio.h>
void main()
{
int num1,num2,num3;
printf("Enter three numbers\n");
scanf("%d %d %d",&num1,&num2,&num3);
if(num1<num2 && num1<num3){
printf("\n%d is the smallest",num1);
}
else if(num2<num3){
printf("\n%d is the smallest",num2);
}
else{
printf("\n%d is the smallest",num3);
}
if(num1>num2 && num1>num3){
printf("\n%d is largest",num1);
}
else if(num2>num3){
printf("\n%d is largest",num2);
}
else{
printf("\n%d is largest",num3);
}
getch();
return 0;
}
Use 2 variables, one to store the smallest, one to store the largest.使用2个变量,一个存储最小的,一个存储最大的。
int min, max;
Then, assign the variable:然后,分配变量:
if (num1 < num2)
min = num1;
if (num3 < min)
min = num3;
printf("%d is the largest number", min);
To know if a number is odd or even, the remainder (also called modulo) of its division by 2 will be 0 (for even) or 1 (for odd):要知道一个数是奇数还是偶数,其除以 2 的余数(也称为模数)将为 0(偶数)或 1(奇数):
int modulo = min % 2;
if (modulo == 0)
printf("%d is even", min);
else
printf("%d is odd", min);
/*Write a program to find the largest and smallest among three entered numbers and
also display whether the identified largest/smallest number is even or odd*/
#include <stdio.h>
#include <conio.h>
void main()
{
// start the programme
int a, b, c, large, small;
printf("Enter three numbers : \n");
scanf("%d%d%d", &a, &b, &c);
if (a > b && a > c)
{
printf("\n%d is largest", a);
large = a;
}
else if (b > c)
{
printf("\n%d is largest", b);
large = b;
}
else
{
printf("\n%d is largest", c);
large = c;
}
if (a < b && a < c)
{
printf("\n%d is smallest", a);
small = a;
}
else if (b < c)
{
printf("\n%d is smallest", b);
small = b;
}
else
{
printf("\n%d is smallest", c);
small = b;
}
if (large % 2 == 0)
{
printf("\n %d is even", large);
}
else
{
printf("\n %d is odd", large);
}
if (small % 2 == 0)
{
printf("\n %d is even", small);
}
else
{
printf("\n %d is odd", small);
}
getch();
// end the programme
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.