简体   繁体   中英

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.

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):

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM