# Check that if large number is divisible by 18

The problem involves checking whether a given large number is divisible by 18. To determine if a number is divisible by 18, we need to ensure that it satisfies two conditions: it should be divisible by 2 and divisible by 9.

## Problem Statement

Given a large number as a string, the task is to check if the number is divisible by 18.

## Idea to Solve the Problem

To solve this problem, we can follow these steps:

1. Check if the last digit of the number is even (0, 2, 4, 6, 8). If it is, then the number is divisible by 2.

2. Calculate the sum of all the digits in the number. If the sum is divisible by 9, then the number is divisible by 9.

3. If both conditions from steps 1 and 2 are satisfied, then the number is divisible by 18.

## Pseudocode

``````class Divisibility
method isDivisibleBy18(num)
result = false
n = length of num
if n == 1 and num.charAt(0) == '0'
result = true
else if n > 1
if last digit of num is even
sum = 0
for i from 0 to n - 1
sum += (num.charAt(i) - 48)
if sum % 9 == 0
result = true
if result is true
print "Given number (" + num + ") is divisible by 18"
else
print "Given number (" + num + ") is not divisible by 18"

function main

## Algorithm Explanation

The `isDivisibleBy18` method checks if a given large number (as a string) is divisible by 18:

1. If the number is 0, it is divisible by 18, so we set `result` to true.
2. If the last digit of the number is even, we calculate the sum of its digits and check if the sum is divisible by 9.
3. If both conditions are satisfied, we set `result` to true.

The `main` function tests the method with different large numbers.

## Program solution

``````// Java program for
// Check that if large number is divisible by 18
public class Divisibility
{
public void isDivisibleBy18(String num)
{
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
boolean result = false;
int n = num.length();
if (n == 1 && num.charAt(0) == '0')
{
// When given number is zero
result = true;
}
else if (n > 1)
{
if (num.charAt(n - 1) == '0' ||
num.charAt(n - 1) == '2' ||
num.charAt(n - 1) == '4' ||
num.charAt(n - 1) == '6' ||
num.charAt(n - 1) == '8')
{
// Number is divisible by 2
int sum = 0;
// Calculate sum of all digit
for (int i = 0; i < n; ++i)
{
sum += (num.charAt(i) - 48);
}
if ((sum % 9) == 0)
{
// When number divisible by 9
result = true;
}
}
}
if (result == true)
{
System.out.print(" Given number (" +
num + ") is divisible by 18\n");
}
else
{
System.out.print(" Given number (" +
num + ") is not divisible by 18\n");
}
}
public static void main(String[] args)
{
// Test
}
}``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````
``````// Include header file
#include <iostream>
#include <string>

using namespace std;
// C++ program for
// Check that if large number is divisible by 18
class Divisibility
{
public: void isDivisibleBy18(string num)
{
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
bool result = false;
int n = num.length();
if (n == 1 && num[0] == '0')
{
// When given number is zero
result = true;
}
else if (n > 1)
{
if (num[n - 1] == '0' ||
num[n - 1] == '2' ||
num[n - 1] == '4' ||
num[n - 1] == '6' ||
num[n - 1] == '8')
{
// Number is divisible by 2
int sum = 0;
// Calculate sum of all digit
for (int i = 0; i < n; ++i)
{
sum += (num[i] - 48);
}
if ((sum % 9) == 0)
{
// When number divisible by 9
result = true;
}
}
}
if (result == true)
{
cout << " Given number ("
<< num << ") is divisible by 18\n";
}
else
{
cout << " Given number ("
<< num << ") is not divisible by 18\n";
}
}
};
int main()
{
// Test
return 0;
}``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````
``````// Include namespace system
using System;
// Csharp program for
// Check that if large number is divisible by 18
public class Divisibility
{
public void isDivisibleBy18(String num)
{
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
Boolean result = false;
int n = num.Length;
if (n == 1 && num[0] == '0')
{
// When given number is zero
result = true;
}
else if (n > 1)
{
if (num[n - 1] == '0' ||
num[n - 1] == '2' ||
num[n - 1] == '4' ||
num[n - 1] == '6' ||
num[n - 1] == '8')
{
// Number is divisible by 2
int sum = 0;
// Calculate sum of all digit
for (int i = 0; i < n; ++i)
{
sum += (num[i] - 48);
}
if ((sum % 9) == 0)
{
// When number divisible by 9
result = true;
}
}
}
if (result == true)
{
Console.Write(" Given number (" + num + ") is divisible by 18\n");
}
else
{
Console.Write(" Given number (" + num + ") is not divisible by 18\n");
}
}
public static void Main(String[] args)
{
// Test
}
}``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````
``````package main
import "fmt"
// Go program for
// Check that if large number is divisible by 18
type Divisibility struct {}
func getDivisibility() * Divisibility {
var me *Divisibility = &Divisibility {}
return me
}
func(this Divisibility) isDivisibleBy18(num string) {
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
var result bool = false
var n int = len(num)
if n == 1 && num[0] == '0' {
// When given number is zero
result = true
} else if n > 1 {
if num[n - 1] == '0' || num[n - 1] == '2' ||
num[n - 1] == '4' || num[n - 1] == '6' ||
num[n - 1] == '8' {
// Number is divisible by 2
var sum int = 0
// Calculate sum of all digit
for i := 0 ; i < n ; i++ {
sum += (int(num[i]) - 48)
}
if (sum % 9) == 0 {
// When number divisible by 9
result = true
}
}
}
if result == true {
fmt.Print(" Given number (",
num, ") is divisible by 18\n")
} else {
fmt.Print(" Given number (",
num, ") is not divisible by 18\n")
}
}
func main() {
var task * Divisibility = getDivisibility()
// Test
}``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````
``````<?php
// Php program for
// Check that if large number is divisible by 18
class Divisibility
{
public	function isDivisibleBy18(\$num)
{
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
\$result = false;
\$n = strlen(\$num);
if (\$n == 1 && \$num[0] == '0')
{
// When given number is zero
\$result = true;
}
else if (\$n > 1)
{
if (\$num[\$n - 1] == '0' ||
\$num[\$n - 1] == '2' ||
\$num[\$n - 1] == '4' ||
\$num[\$n - 1] == '6' ||
\$num[\$n - 1] == '8')
{
// Number is divisible by 2
\$sum = 0;
// Calculate sum of all digit
for (\$i = 0; \$i < \$n; ++\$i)
{
\$sum += (ord(\$num[\$i]) - 48);
}
if ((\$sum % 9) == 0)
{
// When number divisible by 9
\$result = true;
}
}
}
if (\$result == true)
{
echo(" Given number (".\$num.
") is divisible by 18\n");
}
else
{
echo(" Given number (".\$num.
") is not divisible by 18\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````
``````// Node JS program for
// Check that if large number is divisible by 18
class Divisibility
{
isDivisibleBy18(num)
{
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
var result = false;
var n = num.length;
if (n == 1 && num.charAt(0) == '0')
{
// When given number is zero
result = true;
}
else if (n > 1)
{
if (num.charAt(n - 1) == '0' ||
num.charAt(n - 1) == '2' ||
num.charAt(n - 1) == '4' ||
num.charAt(n - 1) == '6' ||
num.charAt(n - 1) == '8')
{
// Number is divisible by 2
var sum = 0;
// Calculate sum of all digit
for (var i = 0; i < n; ++i)
{
sum += (num.charAt(i).charCodeAt(0) - 48);
}
if ((sum % 9) == 0)
{
// When number divisible by 9
result = true;
}
}
}
if (result == true)
{
process.stdout.write(" Given number (" + num + ") is divisible by 18\n");
}
else
{
process.stdout.write(" Given number (" + num + ") is not divisible by 18\n");
}
}
}

function main()
{
// Test
}
main();``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````
``````#  Python 3 program for
#  Check that if large number is divisible by 18
class Divisibility :
def isDivisibleBy18(self, num) :
#  Condition
#  ➀  Number should be divisible by 2
#  ➁  Number should be divisible by 9
result = False
n = len(num)
if (n == 1 and num[0] == '0') :
#  When given number is zero
result = True
elif (n > 1) :
if (num[n - 1] == '0'
or num[n - 1] == '2'
or num[n - 1] == '4'
or num[n - 1] == '6'
or num[n - 1] == '8') :
#  Number is divisible by 2
sum = 0
i = 0
#  Calculate sum of all digit
while (i < n) :
sum += (ord(num[i]) - 48)
i += 1

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

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

def main() :
#  Test

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

#### Output

`````` Given number ( 34534564565 ) is not divisible by 18
Given number ( 32473248726872398347612 ) is divisible by 18
Given number ( 234234242 ) is not divisible by 18
Given number ( 565678678435345353465475654626 ) is divisible by 18``````
``````#  Ruby program for
#  Check that if large number is divisible by 18
class Divisibility
def isDivisibleBy18(num)
#  Condition
#  ➀  Number should be divisible by 2
#  ➁  Number should be divisible by 9
result = false
n = num.length
if (n == 1 && num[0] == '0')
#  When given number is zero
result = true
elsif (n > 1)
if (num[n - 1] == '0' ||
num[n - 1] == '2' ||
num[n - 1] == '4' ||
num[n - 1] == '6' ||
num[n - 1] == '8')
#  Number is divisible by 2
sum = 0
i = 0
#  Calculate sum of all digit
while (i < n)
sum += (num[i].ord - 48)
i += 1
end

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

end

end

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

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

end

end

def main()
#  Test
end

main()``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18
``````
``````import scala.collection.mutable._;
// Scala program for
// Check that if large number is divisible by 18
class Divisibility()
{
def isDivisibleBy18(num: String): Unit = {
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
var result: Boolean = false;
var n: Int = num.length();
if (n == 1 && num.charAt(0) == '0')
{
// When given number is zero
result = true;
}
else if (n > 1)
{
if (num.charAt(n - 1) == '0' ||
num.charAt(n - 1) == '2' ||
num.charAt(n - 1) == '4' ||
num.charAt(n - 1) == '6' ||
num.charAt(n - 1) == '8')
{
// Number is divisible by 2
var sum: Int = 0;
var i: Int = 0;
// Calculate sum of all digit
while (i < n)
{
sum += (num.charAt(i).toInt - 48);
i += 1;
}
if ((sum % 9) == 0)
{
// When number divisible by 9
result = true;
}
}
}
if (result == true)
{
print(" Given number (" + num + ") is divisible by 18\n");
}
else
{
print(" Given number (" + num + ") is not divisible by 18\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Divisibility = new Divisibility();
// Test
}
}``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````
``````import Foundation;
// Swift 4 program for
// Check that if large number is divisible by 18
class Divisibility
{
func isDivisibleBy18(_ v: String)
{
let num = Array(v);
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
var result: Bool = false;
let n: Int = num.count;
if (n == 1 && num[0] == "0")
{
// When given number is zero
result = true;
}
else if (n > 1)
{
if (num[n - 1] == "0" ||
num[n - 1] == "2" ||
num[n - 1] == "4" ||
num[n - 1] == "6" ||
num[n - 1] == "8")
{
// Number is divisible by 2
var sum: Int = 0;
var i: Int = 0;
// Calculate sum of all digit
while (i < n)
{
sum += (Int(UnicodeScalar(String(num[i]))!.value) - 48);
i += 1;
}
if ((sum % 9) == 0)
{
// When number divisible by 9
result = true;
}
}
}
if (result == true)
{
print(" Given number (", v ,") is divisible by 18");
}
else
{
print(" Given number (", v ,") is not divisible by 18");
}
}
}
func main()
{
// Test
}
main();``````

#### Output

`````` Given number ( 34534564565 ) is not divisible by 18
Given number ( 32473248726872398347612 ) is divisible by 18
Given number ( 234234242 ) is not divisible by 18
Given number ( 565678678435345353465475654626 ) is divisible by 18``````
``````// Kotlin program for
// Check that if large number is divisible by 18
class Divisibility
{
fun isDivisibleBy18(num: String): Unit
{
// Condition
// ➀  Number should be divisible by 2
// ➁  Number should be divisible by 9
var result: Boolean = false;
val n: Int = num.length;
if (n == 1 && num.get(0) == '0')
{
// When given number is zero
result = true;
}
else if (n > 1)
{
if (num.get(n - 1) == '0' ||
num.get(n - 1) == '2' ||
num.get(n - 1) == '4' ||
num.get(n - 1) == '6' ||
num.get(n - 1) == '8')
{
// Number is divisible by 2
var sum: Int = 0;
var i: Int = 0;
// Calculate sum of all digit
while (i < n)
{
sum += (num.get(i).toInt() - 48);
i += 1;
}
if ((sum % 9) == 0)
{
// When number divisible by 9
result = true;
}
}
}
if (result == true)
{
print(" Given number (" + num + ") is divisible by 18\n");
}
else
{
print(" Given number (" + num + ") is not divisible by 18\n");
}
}
}
fun main(args: Array < String > ): Unit
{
// Test
}``````

#### Output

`````` Given number (34534564565) is not divisible by 18
Given number (32473248726872398347612) is divisible by 18
Given number (234234242) is not divisible by 18
Given number (565678678435345353465475654626) is divisible by 18``````

## Time Complexity

The time complexity of this algorithm is O(n), where n is the number of digits in the given large number.

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