N
直到 0N is a multiple of 11.
N is not a multiple of 11.
n
(因為整數存不下) 直到 0
char n[1000] = {0};
while(scanf("%s", n)){
if(*n == '0' && strlen(n) == 1){
break;
}
else{
...
}
}
for
迴圈及迴圈變數 i
控制一加一減每個位數,要記得是字元所以要 - '0'
int i;
int difference = 0;
for(i = 0; i < strlen(n); i++){
if(i % 2 == 0){
difference = difference + (n[i] - '0');
}
else{
difference = difference - (n[i] - '0');
}
}
if(difference % 11 == 0){
flag = true;
}
if(flag){
printf("%s is a multiple of 11.\n", n);
}
else{
printf("%s is not a multiple of 11.\n", n);
}
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
int main(){
char n[1000] = {0};
while(scanf("%s", n)){
if(*n == '0' && strlen(n) == 1){
break;
}
else{
int i;
bool flag = false;
int difference = 0;
for(i = 0; i < strlen(n); i++){
if(i % 2 == 0){
difference = difference + (n[i] - '0');
}
else{
difference = difference - (n[i] - '0');
}
}
if(difference % 11 == 0){
flag = true;
}
if(flag){
printf("%s is a multiple of 11.\n", n);
}
else{
printf("%s is not a multiple of 11.\n", n);
}
}
}
return 0;
}