C程序检查数字是否可以被任何数字整除

给定数字n,任务是查找数字中的任何数字是否将数字完全除。就像我们得到的数字128625被5整除,该数字也存在于数字中。

示例

Input: 53142
Output: yes
Explanation: This number is divisible by 1, 2 and 3
which are the digits of the number
Input: 223
Output: No
Explanation: The number is not divisible by either 2 or 3

下面使用的方法如下-

  • 我们将从单位所在的地方开始,并获取单位所在的号码。

  • 检查数字是否可整

  • 用10除数

  • 转到步骤1,直到数字为0

算法

Start
In function int divisible(long long int n)
   Step 1-> Declare and initialize temp = n
   Step 2 -> Loop while n {
      Set k as n % 10
      If temp % k == 0 then,
         Return 1
      Set n = n/ 10
   End loop
   Return 0
In Function int main()   Step 1-> Declare and initialize n = 654123
   Step 2-> If (divisible(n)) then,
      Print "Yes”
   Step 3-> Else
   Print "No”

示例

#include <stdio.h>
int divisible(long long int n) {
   long long int temp = n;
   //检查数字是否除以n-
   while (n) {
      int k = n % 10;
      if (temp % k == 0)
         return 1;
         n /= 10;
   }
   return 0;
}
int main() {
   long long int n = 654123;
   if (divisible(n)) {
      printf("Yes\n");
   }
   else
      printf("No\n");
   return 0;
}

输出结果

如果运行上面的代码,它将生成以下输出-

Yes