首页  >  文章  >  后端开发  >  C程序检查一个数字的所有位数是否能整除它

C程序检查一个数字的所有位数是否能整除它

PHPz
PHPz转载
2023-09-19 23:21:031006浏览

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

以上是C程序检查一个数字的所有位数是否能整除它的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除