给定数字N。任务是检查给定数字的位数总和是否除以该数字。如果它被除,则打印“是”,否则打印“否”。
例子:
Input : N = 12
Output : YES
Sum of digits = 1+2 =3 and 3 divides 12.
So, print YES.
Input : N = 15
Output : NO
提取数字的位数并计算所有数字的总和,然后检查数字总和是否为N。
下面是上述方法的实现:
C++
// C++ program to check if sum of
// digits of a number divides it
#include
using namespace std;
// Function to check if sum of
// digits of a number divides it
int 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 Code
int 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 it
import java.util.*;
import java.lang.*;
class GFG
{
// Function to check if sum of
// digits of a number divides it
static 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 Code
public 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 it
def 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
if __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 it
using System;
// Function to check if sum of
// digits of a number divides it
class 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 Code
public 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
输出:
YES