Home  >  Article  >  Backend Development  >  C program to check if all the digits of a number can divide it

C program to check if all the digits of a number can divide it

PHPz
PHPzforward
2023-09-19 23:21:031062browse

C program to check if all the digits of a number can divide it

For a given number n, we need to find out whether all digits of n are divisible by it, i.e. if a number is 'xy', then both x and y should be divisible it.

Example

Input - 24

Output - Yes

Explanation - 24 % 2 == 0, 24 % 4 == 0

Use conditional statements to check whether each number is non-zero and divisible by the number. We need to iterate over each number and check if that number is divisible by the given number.

Example

#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;
}

Output

The number is divisible by its digits

The above is the detailed content of C program to check if all the digits of a number can divide it. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete