[英]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>
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;
}
使用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);
/*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.