Skip to content
Related Articles
Open in App
Not now

Related Articles

Check if a large number is divisibility by 15

Improve Article
Save Article
Like Article
  • Difficulty Level : Easy
  • Last Updated : 24 Mar, 2023
Improve Article
Save Article
Like Article

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

Brute Force Approach:

  1. Convert the input number to a string and remove any whitespace or special characters from it.
  2. Initialize a variable sum to 0.
  3. Iterate over each character in the string and convert it to an integer.
  4. Add the integer to the sum.
  5. After iterating over all the characters, check if the sum is divisible by both 3 and 5. If it is, return “Yes”, otherwise return “No”.
     

Below is implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
 
string check_divisibility_by_15(string num) {
    // Remove whitespace and special characters from input string
    num.erase(remove_if(num.begin(), num.end(), [](char c) { return !isdigit(c); }), num.end());
 
    // Initialize sum to 0
    int sum = 0;
 
    // Iterate over each character and add its integer value to sum
    for (char c : num) {
        sum += (int)(c - '0');
    }
 
    // Check if sum is divisible by 3 and 5
    if (sum % 3 == 0 && sum % 5 == 0) {
        return "Yes";
    } else {
        return "No";
    }
}
 
int main() {
    // Test the function with some sample inputs
    string num1 = "31";
    string num2 = "156457463274623847239840239402394085458848462385346236482374823647643742374523747264723762374620";
    cout << check_divisibility_by_15(num1) << endl; // Output: No
    cout << check_divisibility_by_15(num2) << endl; // Output: Yes
 
    return 0;
}

Output

No
Yes

Time Complexity: O(n)

Auxiliary Space: O(1)

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




// 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 15
bool 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 program
int 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




// Java program to check if a large
// number is divisible by 15
import java.util.*;
 
class GFG
{
      
// function to check if a large
// number is divisible by 15
public 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 code
public 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




# Python3 program to check if
# a large number is
# divisible by 15
 
# to find sum
def 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 15
def 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 Code
s = "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#




// C# program to check if a large
// number is divisible by 15
using System;
 
class GFG
{
// function to check if a large
// number is divisible by 15
public 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 code
public 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
// PHP program to check if
// a large number is
// divisible by 15
 
// to find sum
function 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 15
function 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
?>

Javascript




<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 15
function 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 Code
let 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) 
Auxiliary space: O(1)

Method 2: Checking given number is divisible by 15 or not by using the modulo division operator “%”. 

C++




#include <iostream>
using namespace std;
int main()
{
    //input
    long long int n=31;
      
      
    // finding given number is divisible by 15 or not
    if (n % 15 == 0)
    {
        cout << "Yes";
    }
    else
    {
        cout << "No";
    }
    
    return 0;
}
 
// This code is contributed by satwik4409.

Java




/*package whatever //do not write package name here */
 
import java.io.*;
 
class GFG {
  public static void main(String[] args)
  {
 
    // input
    long n = 31;
 
     
    // finding given number is
    // divisible by 15 or not
    if (n % 15 == 0) {
      System.out.println("Yes");
    }
    else {
      System.out.println("No");
    }
  }
}
 
// This code is contributed by laxmigangarajula03.

Python3




# Python code
# To check whether the given number is divisible by 15 or not
 
#input
n=31
# the above input can also be given as n=input() -> taking input from user
# finding given number is divisible by 15 or not
if int(n)%15==0:
  print("Yes")
else:
  print("No")
 
  # this code is contributed by gangarajula laxmi

C#




using System;
public class GFG {
 
  static public void Main()
  {
 
    // input
    long n = 31;
 
     
    //finding given number is
    // divisible by 15 or not
    if (n % 15 == 0) {
      Console.Write("Yes");
    }
    else {
      Console.Write("No");
    }
  }
}
 
// This code is contributed by laxmigangarajula03

Javascript




<script>
       // JavaScript code for the above approach
       // To check whether the given number is divisible by 15 or not
 
       //input
       var n = 31
        
       // finding given number is divisible by 15 or not
       if (n % 15 == 0)
           document.write("Yes")
       else
           document.write("No")
 
 
   // This code is contributed by Potta Lokesh
   </script>

PHP




<?php
    $num =31;
    // checking if the given number is divisible by 15 or
    // not using modulo division operator if the output of
    // num%15 is equal to 0 then given number is divisible
    // by 15 otherwise not divisible by15
    if ($num % 15 == 0) {
        echo "Yes";
    }
    else {
        echo "No";
    }
   
?>

Output

No

Time complexity: O(1) as it is doing constant operations
Auxiliary Space: O(1) as it is using constant space for variables

This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
 


My Personal Notes arrow_drop_up
Like Article
Save Article
Related Articles

Start Your Coding Journey Now!