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

21 May, 2021
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++
Java
Python 3
C#
PHP
Javascript
// 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 numberint 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 notbool Divisible(string str, int n){    if (SumOfDigits(str, n) % 3 == 0 and str[n - 1] == '0')        return true;     return false;} // Driver codeint main(){    string str = "263730746028908374890";     int n = str.size();     if (Divisible(str, n))        cout << "YES";    else        cout << "NO";     return 0;}
// 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 numberstatic 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 notstatic boolean Divisible(String str,                         int n){    if (SumOfDigits(str, n) % 3 == 0 &&        str.charAt(n - 1) == '0')        return true;     return false;} // Driver codepublic 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 program to Check if# a large number is# divisible by 2, 3 and 5 or not. # function to return sum of digits of# a numberdef 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 notdef Divisible(str, n):    if ((SumOfDigits(str, n) % 3 == 0 and        str[n - 1] == '0')):        return True     return False # Driver codeif __name__ == "__main__":    str = "263730746028908374890"     n = len(str)     if (Divisible(str, n)):        print("YES")    else:        print("NO")         # this code is contributed by# ChitraNayal
// 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 numberstatic 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 notstatic bool Divisible(String str, int n){    if (SumOfDigits(str, n) % 3 == 0 &&                    str[n - 1] == '0')        return true;     return false;} // Driver codepublic 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// 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 numberfunction 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 notfunction 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?>
<script> // JavaScript program to Check if a large// number is divisible by 2, 3 and// 5 or not. // Function to return sum of// digits of a numberfunction SumOfDigits(str, n){    var sum = 0;     for(var i = 0; i < n; i++)        sum += (str.charAt(i) - '0');     return sum;} // Function to Check if a large number// is divisible by 2, 3 and 5 or notfunction Divisible(str, n){    if (SumOfDigits(str, n) % 3 == 0 &&        str.charAt(n - 1) == '0')        return true;     return false;} // Driver codevar str = "263730746028908374890";var n = str.length; if (Divisible(str, n))    document.write("YES");else    document.write("NO"); // This code is contributed by Ankita saini </script>
YES
 
ihritik
Shivi_Aggarwal
ukasp
ankita_saini
divisibility
large-numbers
Competitive Programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Prefix Sum Array - Implementation and Applications in Competitive Programming
Ordered Set and GNU C++ PBDS
Modulo 10^9+7 (1000000007)
Bits manipulation (Important tactics)
What is Competitive Programming and How to Prepare for It?
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7