C Program to Check if all digits of a number divide it


For a number n given, we need to find whether all digits of n divide it or not i.e. if a number is ‘xy’ then both x and y should divide it.

Sample

Input - 24 

Output - Yes 

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

Using conditional statements checking if each digit is non-zero and divides the number. We need to iterate over each digit of the number. And the check for the divisibility of the number for that 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

Updated on: 09-Aug-2019

488 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements