Check if the sum of digits of a number N divides it
Given a number N. The task is to check if the sum of digits of the given number divides the number or not. If it divides it then print YES otherwise print NO.
Examples:
Input : N = 12 Output : YES Sum of digits = 1+2 =3 and 3 divides 12. So, print YES. Input : N = 15 Output : NO
Extract digits of the number and calculate the sum of all of its digits and check if the sum of digits dives N or not.
Below is the implementation of the above approach:
C++
// C++ program to check if sum of// digits of a number divides it#include <iostream>using namespace std;// Function to check if sum of// digits of a number divides itint isSumDivides(int N){ int temp = N; int sum = 0; // Calculate sum of all of digits of N while (temp) { sum += temp % 10; temp /= 10; } if (N % sum == 0) return 1; else return 0;}// Driver Codeint main(){ int N = 12; if (isSumDivides(N)) cout << "YES"; else cout << "NO"; return 0;} |
Java
// Java program to check if sum of// digits of a number divides itimport java.util.*;import java.lang.*;class GFG{// Function to check if sum of// digits of a number divides itstatic int isSumDivides(int N){ int temp = N; int sum = 0; // Calculate sum of all of digits of N while (temp > 0) { sum += temp % 10; temp /= 10; } if (N % sum == 0) return 1; else return 0;}// Driver Codepublic static void main(String args[]){ int N = 12; if (isSumDivides(N) == 1) System.out.print("YES"); else System.out.print("NO");}}// This code is contributed// by Akanksha Rai(Abby_akku) |
Python3
# Python3 program to check if sum of# digits of a number divides it# Function to check if sum of# digits of a number divides itdef isSumDivides(N): temp = N sum = 0 # Calculate sum of all of # digits of N while (temp): sum += temp % 10 temp = int(temp / 10) if (N % sum == 0): return 1 else: return 0# Driver Codeif __name__=='__main__': N = 12 if (isSumDivides(N)): print("YES") else: print("NO") # This code is contributed by# mits |
C#
// C# program to check if sum of// digits of a number divides itusing System;// Function to check if sum of// digits of a number divides itclass GFG{public int isSumDivides(int N){ int temp = N, sum = 0; // Calculate sum of all of // digits of N while (temp > 0) { sum += temp % 10; temp /= 10; } if (N % sum == 0) return 1; else return 0;}// Driver Codepublic static void Main(){ GFG g = new GFG(); int N = 12; if (g.isSumDivides(N) > 0) Console.WriteLine("YES"); else Console.WriteLine("NO");}}// This code is contributed by Soumik |
PHP
<?php// PHP program to check if sum of// digits of a number divides it// Function to check if sum of// digits of a number divides itfunction isSumDivides($N){ $temp = $N; $sum = 0; // Calculate sum of all of // digits of N while ($temp) { $sum += $temp % 10; $temp = (int)$temp / 10; } if ($N % $sum == 0) return 1; else return 0;}// Driver Code$N = 12;if (isSumDivides($N)) echo "YES";else echo "NO"; // This code is contributed by ajit?> |
Javascript
<script>// javascript program to check if sum of// digits of a number divides it // Function to check if sum of // digits of a number divides it function isSumDivides(N) { var temp = N; var sum = 0; // Calculate sum of all of digits of N while (temp > 0) { sum += temp % 10; temp = parseInt(temp/10); } if (N % sum == 0) return 1; else return 0; } // Driver Code var N = 12; if (isSumDivides(N) == 1) document.write("YES"); else document.write("NO");// This code contributed by aashish1995</script> |
Output:
YES
Attention reader! Don’t stop learning now. Join the First-Step-to-DSA Course for Class 9 to 12 students , specifically designed to introduce data structures and algorithms to the class 9 to 12 students


