C语言 一种程序,用于查找三个输入数字中的最大和最小数字,并显示所识别的最大/最小数字是偶数还是奇数

wmomyfyw  于 2022-12-11  发布在  其他
关注(0)|答案(2)|浏览(182)

这是我的家庭作业,我一直在想我应该如何识别最小/最大的数字是偶数还是奇数。

#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;
}
r7xajy2e

r7xajy2e1#

使用2个变量,一个存储最小值,一个存储最大值。

int min, max;

然后,指定变数:

if (num1 < num2)
    min = num1;
if (num3 < min)
    min = num3;
printf("%d is the largest number", min);

要知道一个数是奇数还是偶数,它除以2的余数(也称为模)将是0(偶数)或1(奇数):

int modulo = min % 2;
if (modulo == 0)
    printf("%d is even", min);
else
    printf("%d is odd", min);
wvt8vs2t

wvt8vs2t2#

/*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
}

相关问题