Check if a large number is divisibility by 15 - GeeksforGeeks

14 Apr, 2021
Given a very large number. Check its divisibility by 15.Examples:  
Input: 31
Output: No

Input : num = "156457463274623847239840239
               402394085458848462385346236
               482374823647643742374523747
               264723762374620"
Output: Yes
Given number is divisible by 15
 
A number is divisible by 15 if it is divisible by 5 (if the last digit is 5 or 0), and it is divisible by 3 (if sum of digits is divisible by 3).Here, used accumulate function to sum up all the numbers.  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to check if a large// number is divisible by 15#include <bits/stdc++.h> using namespace std; // function to check if a large number// is divisible by 15bool isDivisible(string s){    // length of string    int n = s.length();     // check divisibility by 5    if (s[n - 1] != '5' and s[n - 1] != '0')        return false;     // Sum of digits    int sum = accumulate(begin(s), end(s), 0) - '0' * n;     // if divisible by 3    return (sum % 3 == 0);} // driver programint main(){    string s = "15645746327462384723984023940239";    isDivisible(s)? cout << "Yes\n": cout << "No\n";    string s1 = "15645746327462384723984023940235";    isDivisible(s1)? cout << "Yes\n": cout << "No\n";    return 0;}
// Java program to check if a large// number is divisible by 15import java.util.*; class GFG{      // function to check if a large// number is divisible by 15public static boolean isDivisible(String S){    // length of string    int n = S.length();         // check divisibility by 5    if (S.charAt(n - 1) != '5' &&        S.charAt(n - 1) != '0')        return false;             // Sum of digits    int sum = 0;    for(int i = 0; i < S.length(); i++)        sum += (int)S.charAt(i);                 // if divisible by 3        if(sum % 3 == 0)            return true;        else            return false;}     // Driver codepublic static void main (String[] args){    String S = "15645746327462384723984023940239";    if(isDivisible(S) == true)        System.out.println("Yes");    else        System.out.println("No");             String S1 = "15645746327462384723984023940235";    if(isDivisible(S1) == true)        System.out.println("Yes");    else        System.out.println("No");    }}         // This code is contributed// by Akanksha Rai(Abby_akku)
# Python3 program to check if# a large number is# divisible by 15 # to find sumdef accumulate(s):    acc = 0;    for i in range(len(s)):        acc += ord(s[i]) - 48;    return acc; # function to check# if a large number# is divisible by 15def isDivisible(s):    # length of string    n = len(s);     # check divisibility by 5    if (s[n - 1] != '5' and s[n - 1] != '0'):        return False;     # Sum of digits    sum = accumulate(s);         # if divisible by 3    return (sum % 3 == 0);  # Driver Codes = "15645746327462384723984023940239";if isDivisible(s):    print("Yes");else:    print("No"); s = "15645746327462384723984023940235";if isDivisible(s):    print("Yes");else:    print("No"); # This code is contributed by mits
// C# program to check if a large// number is divisible by 15using System; class GFG{// function to check if a large// number is divisible by 15public static bool isDivisible(String S){    // length of string    int n = S.Length;         // check divisibility by 5    if (S[n - 1] != '5' &&        S[n - 1] != '0')        return false;             // Sum of digits    int sum = 0;    for(int i = 0; i < S.Length; i++)        sum += (int)S[i];                 // if divisible by 3        if(sum % 3 == 0)            return true;        else            return false;}     // Driver codepublic static void Main(){    String S = "15645746327462384723984023940239";    if(isDivisible(S) == true)        Console.WriteLine("Yes");    else        Console.WriteLine("No");             String S1 = "15645746327462384723984023940235";    if(isDivisible(S1) == true)        Console.WriteLine("Yes");    else        Console.WriteLine("No");    }}         // This code is contributed// by mits
<?php// PHP program to check if// a large number is// divisible by 15 // to find sumfunction accumulate($s){    $acc = 0;    for($i = 0;        $i < strlen($s); $i++)    {        $acc += $s[$i] - '0';    }    return $acc;} // function to check// if a large number// is divisible by 15function isDivisible($s){    // length of string    $n = strlen($s);     // check divisibility by 5    if ($s[$n - 1] != '5' &&        $s[$n - 1] != '0')        return false;     // Sum of digits    $sum = accumulate($s);         // if divisible by 3    return ($sum % 3 == 0);} // Driver Code$s = "15645746327462384723984023940239";isDivisible($s) ?print("Yes\n") : print("No\n"); $s = "15645746327462384723984023940235";isDivisible($s) ?print("Yes\n") : print("No\n"); // This code is contributed by mits?>
<script>// Javascript program to check if// a large number is// divisible by 15// to find sum function accumulate(s){    let acc = 0;    for(let i = 0;        i < s.length; i++)    {        acc += s[i] - '0';    }    return acc;} // function to check// if a large number// is divisible by 15function isDivisible(s){     // length of string    let n = s.length;     // check divisibility by 5    if (s[n - 1] != '5' &&        s[n - 1] != '0')        return false;     // Sum of digits    let sum = accumulate(s);         // if divisible by 3    return (sum % 3 == 0);} // Driver Codelet s = "15645746327462384723984023940239";isDivisible(s) ?document.write("Yes<br>") : document.write("No<br>"); s = "15645746327462384723984023940235";isDivisible(s) ?document.write("Yes<br>") : document.write("No<br>"); // This code is contributed by _saurabh_jaiswal</script>
Output:  
No
Yes
Time complexity: O(number of digits)This article is contributed by Striver. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Mithun Kumar
Akanksha_Rai
_saurabh_jaiswal
divisibility
large-numbers
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Check if a number is Palindrome
Program to multiply two matrices
Merge two sorted arrays with O(1) extra space
Generate all permutation of a set in Python
Count ways to reach the n'th stair