GeeksforGeeks App
Open App
Browser
Continue

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

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

## C

 // C program to Check if a large number is// divisible by 2, 3 and 5 or not.#include #include #include  // function to return sum of digits of// a numberint SumOfDigits(char 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(char str[], int n){    if (SumOfDigits(str, n) % 3 == 0 && str[n - 1] == '0')        return true;     return false;} // Driver codeint main(){    char str[] = "263730746028908374890";     int n = strlen(str);     if (Divisible(str, n))        printf("YES");    else        printf("NO");     return 0;} // This code is contributed by kothavvsaakash.

## Java

 // Java program to Check if a large// number is divisible by 2, 3 and// 5 or not.import java.io.*;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

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

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



## Javascript



Output:

YES

Time Complexity: O(n), where n is the size of the given string str
Auxiliary Space: O(1), as no extra space is required

#### Another approach:

Approach:

1. Check if the last digit of the number is even. If it is, then the number is divisible by 2.
2. Check if the sum of the digits of the number is divisible by 3. If it is, then the number is divisible by 3.
3. Check if the last digit of the number is 0 or 5. If it is, then the number is divisible by 5.

## C++

 #include  using namespace std; int main(){    long long int n = 1234567890123456789; // example number    int sum = 0;    int last_digit = n % 10;    // Check for divisibility by 2    if (last_digit % 2 == 0) {        cout << n << " is divisible by 2\n";    }    else {        cout << n << " is not divisible by 2\n";    }     // Check for divisibility by 3    long long int temp_n = n;    while (temp_n > 0) {        sum += temp_n % 10;        temp_n /= 10;    }    if (sum % 3 == 0) {        cout << n << " is divisible by 3\n";    }    else {        cout << n << " is not divisible by 3\n";    }     // Check for divisibility by 5    if (last_digit == 0 || last_digit == 5) {        cout << n << " is divisible by 5\n";    }    else {        cout << n << " is not divisible by 5\n";    }     return 0;}

## C

 #include  int main() {    long long int n = 1234567890123456789; // example number    int sum = 0;    int last_digit = n % 10;     // Check for divisibility by 2    if (last_digit % 2 == 0) {        printf("%lld is divisible by 2\n", n);    } else {        printf("%lld is not divisible by 2\n", n);    }     // Check for divisibility by 3    while (n > 0) {        sum += n % 10;        n /= 10;    }    if (sum % 3 == 0) {        printf("%lld is divisible by 3\n", n);    } else {        printf("%lld is not divisible by 3\n", n);    }     // Check for divisibility by 5    if (last_digit == 0 || last_digit == 5) {        printf("%lld is divisible by 5\n", n);    } else {        printf("%lld is not divisible by 5\n", n);    }     return 0;}

## Java

 // Java Equivalentpublic class Main {    public static void main(String[] args) {        long n = 1234567890123456789L; // example number        int sum = 0;        int last_digit = (int) n % 10;               // Check for divisibility by 2        if (last_digit % 2 == 0) {            System.out.println(n + " is divisible by 2");        } else {            System.out.println(n + " is not divisible by 2");        }         // Check for divisibility by 3        long temp_n = n;        while (temp_n > 0) {            sum += temp_n % 10;            temp_n /= 10;        }        if (sum % 3 == 0) {            System.out.println(n + " is divisible by 3");        } else {            System.out.println(n + " is not divisible by 3");        }         // Check for divisibility by 5        if (last_digit == 0 || last_digit == 5) {            System.out.println(n + " is divisible by 5");        } else {            System.out.println(n + " is not divisible by 5");        }    }}

## Python3

 n = 1234567890123456789  # example numbersum = 0last_digit = n % 10 # Check for divisibility by 2if last_digit % 2 == 0:    print(n, "is divisible by 2")else:    print(n, "is not divisible by 2") # Check for divisibility by 3temp_n = nwhile temp_n > 0:    sum += temp_n % 10    temp_n //= 10if sum % 3 == 0:    print(n, "is divisible by 3")else:    print(n, "is not divisible by 3") # Check for divisibility by 5if last_digit == 0 or last_digit == 5:    print(n, "is divisible by 5")else:    print(n, "is not divisible by 5")

## C#

 using System; class Gfg {  public static void Main() {    long n = 1234567890123456789; // example number    int sum = 0;    int last_digit = (int)(n % 10);     // Check for divisibility by 2    if (last_digit % 2 == 0) {      Console.WriteLine(\$"{n} is divisible by 2");    } else {      Console.WriteLine(\$"{n} is not divisible by 2");    }     // Check for divisibility by 3    long temp_n = n;    while (temp_n > 0) {      sum += (int)(temp_n % 10);      temp_n /= 10;    }    if (sum % 3 == 0) {      Console.WriteLine(\$"{n} is divisible by 3");    } else {      Console.WriteLine(\$"{n} is not divisible by 3");    }     // Check for divisibility by 5    if (last_digit == 0 || last_digit == 5) {      Console.WriteLine(\$"{n} is divisible by 5");    } else {      Console.WriteLine(\$"{n} is not divisible by 5");    }  }}

## Javascript

 let n = 1234567890123456789; // example number // Check for divisibility by 2if (n % 2 === 0) {  console.log(`\${n} is divisible by 2`);} else {  console.log(`\${n} is not divisible by 2`);} // Check for divisibility by 3let sum = 0;let temp_n = n;while (temp_n > 0) {  sum += temp_n % 10;  temp_n = Math.floor(temp_n / 10);}if (sum % 3 === 0) {  console.log(`\${n} is divisible by 3`);} else {  console.log(`\${n} is not divisible by 3`);} // Check for divisibility by 5let last_digit = n % 10;if (last_digit === 0 || last_digit === 5) {  console.log(`\${n} is divisible by 5`);} else {  console.log(`\${n} is not divisible by 5`);}

Output

1234567890123456789 is not divisible by 2
0 is divisible by 3
0 is not divisible by 5

Time Complexity: O(log10(n)), where n is the number being checked, because we iterate over each digit of the number once.

Space Complexity: O(1), because we only need a few variables to store the state of the algorithm

My Personal Notes arrow_drop_up