....

2014年5月15日 星期四

[長知識] C語言 - 新手篇章 - 條件判斷式,判斷奇數或偶數

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) {

//條件判斷式 if else, while, ...

int input;

printf("請輸入一個整數:");
scanf("%i", &input);



// input 除以2,餘數等於0,就是偶數
// 當然啦,如果input初以2於1的話,就是奇數

if(input%2==0){
printf("您輸入的值%i是偶數", input);
}else{
printf("您輸入的值%i是奇數", input);
}



//也可以寫成這樣,因為 if(input%2) = if(1)

if(input%2){
printf("您輸入的值%i是奇數", input);
}else{
printf("您輸入的值%i是偶數", input);
}



//也可以寫成這樣,用!作反向
if(!(input%2)){
printf("您輸入的值%i是偶數", input);
}else{
printf("您輸入的值%i是奇數", input);
}

return 0;

}

沒有留言:

張貼留言