Given a number, the task is to check if a number is divisible by 2, 3, and 5 or not. The input number may be large and it may not be possible to store even if we use long long int, so the number is taken as a string.
Examples:
Input : str = "725"
Output : NO
Input : str = "263730746028908374890"
Output : YES
A number is divisible by 2 if it’s right most digit is even and also a number is divisible by 5 if it’s right most digit is zero or five.
So, from above two observations, one can conclude that for the number to be divisible by both 2 and 5 the rightmost digit of the number must be zero.
Now, a number is divisible by 3 if the sum of its digits is divisible by three.
Therefore, a number will be divisible by all of 2, 3, and 5 if:
- Its rightmost digit is zero.
- Sum of all of its digits is divisible by 3.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int SumOfDigits(string str, int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
bool Divisible(string str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 and str[n - 1] == '0')
return true;
return false;
}
int main()
{
string str = "263730746028908374890";
int n = str.size();
if (Divisible(str, n))
cout << "YES";
else
cout << "NO";
return 0;
}
|
C
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int SumOfDigits(char str[], int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
bool Divisible(char str[], int n)
{
if (SumOfDigits(str, n) % 3 == 0 && str[n - 1] == '0')
return true;
return false;
}
int main()
{
char str[] = "263730746028908374890";
int n = strlen(str);
if (Divisible(str, n))
printf("YES");
else
printf("NO");
return 0;
}
|
Java
class GFG
{
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;
}
static boolean Divisible(String str,
int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str.charAt(n - 1) == '0')
return true;
return false;
}
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");
}
}
|
Python 3
def SumOfDigits(str, n):
sum = 0
for i in range(0,n):
sum += int(ord(str[i] )- ord('0'))
return sum
def Divisible(str, n):
if ((SumOfDigits(str, n) % 3 == 0 and
str[n - 1] == '0')):
return True
return False
if __name__ == "__main__":
str = "263730746028908374890"
n = len(str)
if (Divisible(str, n)):
print("YES")
else:
print("NO")
|
C#
using System;
class GFG
{
static int SumOfDigits(String str,
int n)
{
int sum = 0;
for (int i = 0; i < n; i++)
sum += (int)(str[i] - '0');
return sum;
}
static bool Divisible(String str, int n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str[n - 1] == '0')
return true;
return false;
}
public static void Main()
{
String str = "263730746028908374890";
int n = str.Length;
if (Divisible(str, n))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
|
PHP
<?php
function SumOfDigits($str, $n)
{
$sum = 0;
for ($i = 0; $i < $n; $i++)
$sum += (int)($str[$i] - '0');
return $sum;
}
function Divisible($str, $n)
{
if (SumOfDigits($str, $n) % 3 == 0 and
$str[$n - 1] == '0')
return true;
return false;
}
$str = "263730746028908374890";
$n = strlen($str);
if (Divisible($str, $n))
echo "YES";
else
echo "NO";
?>
|
Javascript
<script>
function SumOfDigits(str, n)
{
var sum = 0;
for(var i = 0; i < n; i++)
sum += (str.charAt(i) - '0');
return sum;
}
function Divisible(str, n)
{
if (SumOfDigits(str, n) % 3 == 0 &&
str.charAt(n - 1) == '0')
return true;
return false;
}
var str = "263730746028908374890";
var n = str.length;
if (Divisible(str, n))
document.write("YES");
else
document.write("NO");
</script>
|
Time Complexity: O(n), where n is the size of the given string str
Auxiliary Space: O(1), as no extra space is required