Posted on by Kalkicode
Code Mathematics

# Check that if large number is divisible by 15

To check if a large number is divisible by 15, you need to determine if it is evenly divisible by 15 without leaving a remainder. A number is divisible by 15 if it is divisible by both 3 and 5.

To check if a large number is divisible by 15, you can follow these steps:

1. Add up the digits of the number.
2. If the sum of the digits is divisible by 3, then the number is divisible by 3.
3. Check if the last digit of the number is either 5 or 0. If it is, then the number is divisible by 5.
4. If the number is divisible by both 3 and 5, then it is divisible by 15.

Alternatively, you can divide the large number by 15 and see if the result is a whole number. If it is, then the number is divisible by 15.

## Pseudocode

``````function isDivisibleBy15(num):
n = length(num)
result = false
if n == 1 and num[0] == '0':
result = true
else if n > 0 and (num[n-1] == '5' or num[n-1] == '0'):
sum = 0
for i from 0 to n-1:
sum = sum + (num[i] - '0')
if sum % 3 == 0:
result = true
if result is true:
print("Given number (" + num + ") is divisible by 15")
else:
print("Given number (" + num + ") is not divisible by 15")
``````

## Algorithm Explanation

1. The function `isDivisibleBy15` takes a string `num` as input.
2. We initialize a boolean variable `result` to false to keep track of divisibility by 15.
3. We check if the number is a single digit '0', in which case it is divisible by 15 and set `result` to true.
4. Otherwise, if the last digit of the number is '5' or '0', it has a chance of being divisible by 5.
5. We then calculate the sum of all the digits in the number using a loop, which checks the ASCII value of each digit and subtracts '0' to convert it into an integer.
6. If the sum is divisible by 3, then the number is also divisible by 3 and, hence, divisible by 15. We set `result` to true in this case.
7. Finally, we print the appropriate message based on the value of `result`.

## Code Solution

``````// Java program for
// Check if number is divisible by 15
public class Divisibility
{
public void isDivisibleBy15(String num)
{
// Number is divisible by 15.
// When is divisible by 3 and by 5.
int n = num.length();
boolean result = false;
if (n == 1 && num.charAt(0) == '0')
{
// When number is zero
result = true;
}
else if (n > 0 && (num.charAt(n - 1) == '5' ||
num.charAt(n - 1) == '0'))
{
// When number is divisible by 5
int sum = 0;
// Sum of all digits of number
for (int i = 0; i < n; ++i)
{
// 48 is ASCII of zero
sum = sum + (num.charAt(i) - 48);
}
if ((sum % 3) == 0)
{
// When number is divisible by 3
result = true;
}
}
if (result)
{
System.out.println(" Given number (" +
num + ") is divisible by 15");
}
else
{
System.out.println(" Given number (" +
num + ") is not divisible by 15");
}
}
public static void main(String[] args)
{
String num1 = "23782368473246823734850";
String num2 = "125";
// Test
}
}``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````
``````// Include header file
#include <iostream>
#include <string>
using namespace std;

// C++ program for
// Check if number is divisible by 15
class Divisibility
{
public: void isDivisibleBy15(string num)
{
// Number is divisible by 15.
// When is divisible by 3 and by 5.
int n = num.length();
bool result = false;
if (n == 1 && num[0] == '0')
{
// When number is zero
result = true;
}
else if (n > 0 &&
(num[n - 1] == '5' || num[n - 1] == '0'))
{
// When number is divisible by 5
int sum = 0;
// Sum of all digits of number
for (int i = 0; i < n; ++i)
{
// 48 is ASCII of zero
sum = sum + (num[i] - 48);
}
if ((sum % 3) == 0)
{
// When number is divisible by 3
result = true;
}
}
if (result)
{
cout << " Given number ("
<< num << ") is divisible by 15" << endl;
}
else
{
cout << " Given number ("
<< num << ") is not divisible by 15" << endl;
}
}
};
int main()
{
string num1 = "23782368473246823734850";
string num2 = "125";
// Test
return 0;
}``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````
``````// Include namespace system
using System;
// Csharp program for
// Check if number is divisible by 15
public class Divisibility
{
public void isDivisibleBy15(String num)
{
// Number is divisible by 15.
// When is divisible by 3 and by 5.
int n = num.Length;
Boolean result = false;
if (n == 1 && num[0] == '0')
{
// When number is zero
result = true;
}
else if (n > 0 &&
(num[n - 1] == '5' || num[n - 1] == '0'))
{
// When number is divisible by 5
int sum = 0;
// Sum of all digits of number
for (int i = 0; i < n; ++i)
{
// 48 is ASCII of zero
sum = sum + (num[i] - 48);
}
if ((sum % 3) == 0)
{
// When number is divisible by 3
result = true;
}
}
if (result)
{
Console.WriteLine(" Given number (" +
num + ") is divisible by 15");
}
else
{
Console.WriteLine(" Given number (" +
num + ") is not divisible by 15");
}
}
public static void Main(String[] args)
{
String num1 = "23782368473246823734850";
String num2 = "125";
// Test
}
}``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````
``````package main
import "fmt"
// Go program for
// Check if number is divisible by 15

func isDivisibleBy15(num string) {
// Number is divisible by 15.
// When is divisible by 3 and by 5.
var n int = len(num)
var result bool = false
if n == 1 && num[0] == '0' {
// When number is zero
result = true
} else if n > 0 && (num[n - 1] == '5' ||
num[n - 1] == '0') {
// When number is divisible by 5
var sum int = 0
// Sum of all digits of number
for i := 0 ; i < n ; i++ {
// 48 is ASCII of zero
sum = sum + (int(num[i]) - 48)
}
if (sum % 3) == 0 {
// When number is divisible by 3
result = true
}
}
if result {
fmt.Println(" Given number (", num, ") is divisible by 15")
} else {
fmt.Println(" Given number (", num, ") is not divisible by 15")
}
}
func main() {

var num1 string = "23782368473246823734850"
var num2 string = "125"
// Test
isDivisibleBy15(num1)
isDivisibleBy15(num2)
}``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````
``````<?php
// Php program for
// Check if number is divisible by 15
class Divisibility
{
public	function isDivisibleBy15(\$num)
{
// Number is divisible by 15.
// When is divisible by 3 and by 5.
\$n = strlen(\$num);
\$result = false;
if (\$n == 1 && \$num[0] == '0')
{
// When number is zero
\$result = true;
}
else if (\$n > 0 && (\$num[\$n - 1] == '5' ||
\$num[\$n - 1] == '0'))
{
// When number is divisible by 5
\$sum = 0;
// Sum of all digits of number
for (\$i = 0; \$i < \$n; ++\$i)
{
// 48 is ASCII of zero
\$sum = \$sum + (ord(\$num[\$i]) - 48);
}
if ((\$sum % 3) == 0)
{
// When number is divisible by 3
\$result = true;
}
}
if (\$result)
{
echo(" Given number (".\$num.
") is divisible by 15\n");
}
else
{
echo(" Given number (".\$num.
") is not divisible by 15\n");
}
}
}

function main()
{
\$num1 = "23782368473246823734850";
\$num2 = "125";
// Test
}
main();``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````
``````// Node JS program for
// Check if number is divisible by 15
class Divisibility
{
isDivisibleBy15(num)
{
// Number is divisible by 15.
// When is divisible by 3 and by 5.
var n = num.length;
var result = false;
if (n == 1 && num.charAt(0) == '0')
{
// When number is zero
result = true;
}
else if (n > 0 && (num.charAt(n - 1) == '5' ||
num.charAt(n - 1) == '0'))
{
// When number is divisible by 5
var sum = 0;
// Sum of all digits of number
for (var i = 0; i < n; ++i)
{
// 48 is ASCII of zero
sum = sum + (num.charAt(i).charCodeAt(0) - 48);
}
if ((sum % 3) == 0)
{
// When number is divisible by 3
result = true;
}
}
if (result)
{
console.log(" Given number (" +
num + ") is divisible by 15");
}
else
{
console.log(" Given number (" +
num + ") is not divisible by 15");
}
}
}

function main()
{
var num1 = "23782368473246823734850";
var num2 = "125";
// Test
}
main();``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````
``````#  Python 3 program for
#  Check if number is divisible by 15
class Divisibility :
def isDivisibleBy15(self, num) :
#  Number is divisible by 15.
#  When is divisible by 3 and by 5.
n = len(num)
result = False
if (n == 1 and num[0] == '0') :
#  When number is zero
result = True
elif (n > 0 and(num[n - 1] == '5'
or num[n - 1] == '0')) :
#  When number is divisible by 5
sum = 0
i = 0
#  Sum of all digits of number
while (i < n) :
#  48 is ASCII of zero
sum = sum + (ord(num[i]) - 48)
i += 1

if ((sum % 3) == 0) :
#  When number is divisible by 3
result = True

if (result) :
print(" Given number (", num ,") is divisible by 15")
else :
print(" Given number (", num ,") is not divisible by 15")

def main() :
num1 = "23782368473246823734850"
num2 = "125"
#  Test

if __name__ == "__main__": main()``````

#### Output

`````` Given number ( 23782368473246823734850 ) is divisible by 15
Given number ( 125 ) is not divisible by 15``````
``````#  Ruby program for
#  Check if number is divisible by 15
class Divisibility
def isDivisibleBy15(num)
#  Number is divisible by 15.
#  When is divisible by 3 and by 5.
n = num.length
result = false
if (n == 1 && num[0] == '0')
#  When number is zero
result = true
elsif (n > 0 && (num[n - 1] == '5' ||
num[n - 1] == '0'))
#  When number is divisible by 5
sum = 0
i = 0
#  Sum of all digits of number
while (i < n)
#  48 is ASCII of zero
sum = sum + (num[i].ord - 48)
i += 1
end

if ((sum % 3) == 0)
#  When number is divisible by 3
result = true
end

end

if (result)
print(" Given number (",
num ,") is divisible by 15", "\n")
else

print(" Given number (",
num ,") is not divisible by 15", "\n")
end

end

end

def main()
num1 = "23782368473246823734850"
num2 = "125"
#  Test
end

main()``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15
``````
``````import scala.collection.mutable._;
// Scala program for
// Check if number is divisible by 15
class Divisibility()
{
def isDivisibleBy15(num: String): Unit = {
// Number is divisible by 15.
// When is divisible by 3 and by 5.
var n: Int = num.length();
var result: Boolean = false;
if (n == 1 && num.charAt(0) == '0')
{
// When number is zero
result = true;
}
else if (n > 0 && (num.charAt(n - 1) == '5' ||
num.charAt(n - 1) == '0'))
{
// When number is divisible by 5
var sum: Int = 0;
var i: Int = 0;
// Sum of all digits of number
while (i < n)
{
// 48 is ASCII of zero
sum = sum + (num.charAt(i).toInt - 48);
i += 1;
}
if ((sum % 3) == 0)
{
// When number is divisible by 3
result = true;
}
}
if (result)
{
println(" Given number (" + num + ") is divisible by 15");
}
else
{
println(" Given number (" + num + ") is not divisible by 15");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisibility = new Divisibility();
var num1: String = "23782368473246823734850";
var num2: String = "125";
// Test
}
}``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````
``````import Foundation;
// Swift 4 program for
// Check if number is divisible by 15
class Divisibility
{
func isDivisibleBy15(_ v: String)
{
let num = Array(v);
// Number is divisible by 15.
// When is divisible by 3 and by 5.
let n: Int = num.count;
var result: Bool = false;
if (n == 1 && num[0] == "0")
{
// When number is zero
result = true;
}
else if (n > 0 && (num[n - 1] == "5" ||
num[n - 1] == "0"))
{
// When number is divisible by 5
var sum: Int = 0;
var i: Int = 0;
// Sum of all digits of number
while (i < n)
{
// 48 is ASCII of zero
sum = sum + (Int(UnicodeScalar(String(num[i]))!.value) - 48);
i += 1;
}
if ((sum % 3) == 0)
{
// When number is divisible by 3
result = true;
}
}
if (result)
{
print(" Given number (", v ,") is divisible by 15");
}
else
{
print(" Given number (", v ,") is not divisible by 15");
}
}
}
func main()
{
let num1: String = "23782368473246823734850";
let num2: String = "125";
// Test
}
main();``````

#### Output

`````` Given number ( 23782368473246823734850 ) is divisible by 15
Given number ( 125 ) is not divisible by 15``````
``````// Kotlin program for
// Check if number is divisible by 15
class Divisibility
{
fun isDivisibleBy15(num: String): Unit
{
// Number is divisible by 15.
// When is divisible by 3 and by 5.
val n: Int = num.length;
var result: Boolean = false;
if (n == 1 && num.get(0) == '0')
{
// When number is zero
result = true;
}
else if (n > 0 && (num.get(n - 1) == '5' ||
num.get(n - 1) == '0'))
{
// When number is divisible by 5
var sum: Int = 0;
var i: Int = 0;
// Sum of all digits of number
while (i < n)
{
// 48 is ASCII of zero
sum = sum + (num.get(i).toInt() - 48);
i += 1;
}
if ((sum % 3) == 0)
{
// When number is divisible by 3
result = true;
}
}
if (result)
{
println(" Given number (" +
num + ") is divisible by 15");
}
else
{
println(" Given number (" +
num + ") is not divisible by 15");
}
}
}
fun main(args: Array < String > ): Unit
{
val num1: String = "23782368473246823734850";
val num2: String = "125";
// Test
}``````

#### Output

`````` Given number (23782368473246823734850) is divisible by 15
Given number (125) is not divisible by 15``````

## Resultant Output Explanation

For the given test cases, the output is as follows:

1. Output for Test Case 1: Given number (23782368473246823734850) is divisible by 15. Explanation: The number "23782368473246823734850" is divisible by both 3 and 5 as the sum of its digits is 159, which is divisible by 3 and the last digit is '0', which makes it divisible by 5 as well.

2. Output for Test Case 2: Given number (125) is not divisible by 15. Explanation: The number "125" is not divisible by 15 as the sum of its digits is 8, which is not divisible by 3, and the last digit is '5', which does not make it divisible by 5.

## Time Complexity

The time complexity of the code is primarily determined by the loop that calculates the sum of the digits. The loop runs for 'n' iterations, where 'n' is the number of digits in the input number. Therefore, the time complexity of the algorithm is O(n).

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post