Check if a large number is divisible by 2, 3 and 5 or not

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to Check if a large number is
// divisible by 2, 3 and 5 or not.
#include <bits/stdc++.h>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP 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
function SumOfDigits($str, $n)
{
    $sum = 0;
  
    for ($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
function Divisible($str, $n)
{
    if (SumOfDigits($str, $n) % 3 == 0 and 
                    $str[$n - 1] == '0')
        return true;
  
    return false;
}
  
// Driver code
$str = "263730746028908374890";
  
$n = strlen($str);
  
if (Divisible($str, $n))
    echo "YES";
else
    echo "NO";
  
// This code is contributed 
// by Shivi_Aggarwal
?>

chevron_right


Output:

YES


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ihritik, Shivi_Aggarwal, Ita_c