Home > Article > Backend Development > 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.
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.
#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
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!