给定一个数字,任务是检查数字是否可以被2、3和5整除。即使我们使用long long int,所以输入数字可能很大,并且可能无法存储,因此该数字被视为字符串。
例子:
Input : str = "725"
Output : NO
Input : str = "263730746028908374890"
Output : YES
如果数字的最右边数字是偶数,则该数字可被2整除;如果数字的最右边数字是零或五,则该数字也可被5整除。
因此,从上面的两个观察值中,可以得出结论:对于要被2和5整除的数字,该数字的最右边的数字必须为零。
现在,如果数字的总和可被3整除,则该数字可被3整除。
因此,如果满足以下条件,则数字可以被2、3和5整除:
- 其最右边的数字为零。
- 其所有数字的总和可被3整除。
下面是上述方法的实现:
C++
// CPP program to Check if a large number is
// divisible by 2, 3 and 5 or not.
#include
using namespace std;
// function to return sum of digits of
// a number
int SumOfDigits(string str, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
// function to Check if a large number is
// divisible by 2, 3 and 5 or not
bool Divisible(string str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 and str[n - 1] == '0')
return true;
return false;
}
// Driver code
int main()
{
string str = "263730746028908374890";
int n = str.size();
if (Divisible(str, n))
cout << "YES";
else
cout << "NO";
return 0;
}
Java
// Java program to Check if a large
// number is divisible by 2, 3 and
// 5 or not.
class GFG
{
// function to return sum of
// digits of a number
static int SumOfDigits(String str,
int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str.charAt(i) - '0');
return sum;
}
// function to Check if a large number
// is divisible by 2, 3 and 5 or not
static boolean Divisible(String str,
int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str.charAt(n - 1) == '0')
return true;
return false;
}
// Driver code
public static void main(String []args)
{
String str = "263730746028908374890";
int n = str.length();
if (Divisible(str, n))
System.out.println("YES");
else
System.out.println("NO");
}
}
// This code is contributed by ihritik
Python 3
# Python 3 program to Check if
# a large number is
# divisible by 2, 3 and 5 or not.
# function to return sum of digits of
# a number
def SumOfDigits(str, n):
sum = 0
for i in range(0,n):
sum += int(ord(str[i] )- ord('0'))
return sum
# function to Check if a large number is
# divisible by 2, 3 and 5 or not
def Divisible(str, n):
if ((SumOfDigits(str, n) % 3 == 0 and
str[n - 1] == '0')):
return True
return False
# Driver code
if __name__ == "__main__":
str = "263730746028908374890"
n = len(str)
if (Divisible(str, n)):
print("YES")
else:
print("NO")
# this code is contributed by
# ChitraNayal
C#
// C# program to Check if a large number
// is divisible by 2, 3 and 5 or not.
using System;
class GFG
{
// function to return sum of digits
// of a number
static int SumOfDigits(String str,
int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
// function to Check if a large number
// is divisible by 2, 3 and 5 or not
static bool Divisible(String str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str[n - 1] == '0')
return true;
return false;
}
// Driver code
public static void Main()
{
String str = "263730746028908374890";
int n = str.Length;
if (Divisible(str, n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed by ihritik
PHP
输出:
YES
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。