Home > Backend Development > C++ > 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
Release: 2023-09-19 23:21:03
forward
1086 people have browsed it

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;
}
Copy after login

Output

The number is divisible by its digits
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template