C 程式檢查數字的每一位是否都能整除此數字
對於給定的數字 n,我們需要找出 n 的每一位是否都可以整除它,即如果某個數字是“xy”,那麼 x 和 y 都應該可以整除它。
示例
輸入 - 24
輸出 - 是
說明 - 24 % 2 == 0,24 % 4 == 0
使用條件語句檢查每一位是不是非零且可以整除這個數字。我們需要遍歷此數字的每一位。並檢查數字是否可以被該數字整除。
示例
#include <stdio.h>
int main(){
int n = 24;
int temp = n;
int flag=1;
while (temp > 0){
int r = n % 10;
if (!(r != 0 && n % r == 0)){
flag=0;
}
temp /= 10;
}
if (flag==1)
printf("The number is divisible by its digits");
else
printf("The number is not divisible by its digits");
return 0;
}輸出
The number is divisible by its digits
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP