
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C Program to check if a number is divisible by sum of its digits
Given a number n we have to check whether the sum of its digits divide the number n or not. To find out we have to sum all the numbers starting from the unit place and then divide the number with the final sum.
Like we have a number “521” so we have to find the sum of its digit that will be “5 + 2 + 1 = 8” but 521 is not divisible by 8 without leaving any remainder.
Let’s take another example “60” where “6+0 = 6” which can divide 60 and will not leave any remainder.
Example
Input: 55 Output: No Explanation: 5+5 = 10; 55 not divisible by 10 Input: 12 Output: Yes Explanation: 1+2 = 3; 12 is divisible by 3
The approach used below is as follows −
To solve this problem we have to fetch each digit from the input and find the sum of each digit of a number, then check whether it is dividing the number or not.
- Take input
- Take each number from the unit place using and add it to a sum variable which should be initially zero
- Divide the input with the sum of the number.
- Return the result.
Algorithm
In function int isDivisible(long int num) Step 1-> Declare and initialize temp = num, sum = 0 Step 2-> Loop While num Declare and initialize k as num % 10 Set sum as sum + k Set num as num / 10 End Loop Step 3-> If temp % sum == 0 then, Return 1 Step 4-> Return 0 End function In main() Step 1-> Declare and initialize num as 55 Step 2-> If isDivisible(num) then, Print "yes " Step 3-> Else Print "no "
Example
#include <stdio.h> // This function will check // whether the given number is divisible // by sum of its digits int isDivisible(long int num) { long int temp = num; // Find sum of digits int sum = 0; while (num) { int k = num % 10; sum = sum + k; num = num / 10; } // check if sum of digits divides num if (temp % sum == 0) return 1; return 0; } int main() { long int num = 55; if(isDivisible(num)) printf("yes
"); else printf("no
"); return 0; }
Output
If run the above code it will generate the following output −
No
- Related Articles
- C Program to check if a number is divisible by any of its digits
- Write a C# program to check if a number is divisible by 2
- C++ program to find largest or equal number of A whose sum of digits is divisible by 4
- Check whether sum of digits at odd places of a number is divisible by K in Python
- Check if a given number divides the sum of the factorials of its digits in C++
- Check if a number is magic (Recursive sum of digits is 1) in C++
- Check if a large number is divisible by 20 in C++
- C/C++ Program to check whether it is possible to make a divisible by 3 number using all digits in an array?
- C Program to Check if all digits of a number divide it
- Check if a number is divisible by all prime divisors of another number in C++
- Check if a number is divisible by 23 or not in C++
- Check if a number is divisible by 41 or not in C++
- Sum of a two-digit number and the number obtained by reversing the digits is always divisible by?
- C/C++ Program to check whether it is possible to make the divisible by 3 number using all digits in an array?
- Check if a large number is divisible by 13 or not in C++

Advertisements