Check if a larger number divisible by 36
Last Updated :
07 Nov, 2023
Given a number, check whether a given number is divisible by 36 or not. The number may be very large and may not fit in any numeric(int, long int, float, etc.) data type.
Examples:
Input : 72
Output : Yes
Input : 244
Output : No
Input : 11322134
Output : No
Input : 92567812197966231384
Output : Yes
A number is divisible by 36 if the number is divisible by 4 and 9
- A number is divisible by 4 if the number formed by its last 2 digits is divisible by 4
- A number is divisible by 9 if the sum of the digits of the number is divisible by 9
Below is the implementation based on the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
string divisibleBy36(string num)
{
int l = num.length();
if (l == 0)
return "No" ;
if (l == 1 && num[0] != '0' )
return "No" ;
int two_digit_num = (num[l-2] - '0' )*10 +
(num[l-1] - '0' ) ;
if (two_digit_num%4 != 0)
return "No" ;
int sum = 0;
for ( int i=0; i<l; i++)
sum += (num[i] - '0' );
if (sum%9 != 0)
return "No" ;
return "Yes" ;
}
int main()
{
string num = "92567812197966231384" ;
cout << divisibleBy36(num);
return 0;
}
|
Java
import java.io.*;
class IsDivisible
{
static boolean divisibleBy36(String num)
{
int l = num.length();
if (l == 0 )
return false ;
if (l == 1 && num.charAt( 0 ) != '0' )
return false ;
int two_digit_num = (num.charAt(l- 2 ) - '0' )* 10 +
(num.charAt(l- 1 ) - '0' ) ;
if (two_digit_num% 4 != 0 )
return false ;
int sum = 0 ;
for ( int i= 0 ; i<l; i++)
sum += (num.charAt(i) - '0' );
if (sum% 9 != 0 )
return false ;
return true ;
}
public static void main (String[] args)
{
String num = "92567812197966231384" ;
if (divisibleBy36(num))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def divisibleBy36(num) :
l = len (num)
if (l = = 0 ) :
return ( "No" )
if (l = = 1 and num[ 0 ] ! = '0' ) :
return ( "No" )
two_digit_num = ((( int )(num[l - 2 ])) *
10 + ( int )(num[l - 1 ]))
if (two_digit_num % 4 ! = 0 ) :
return "No"
sm = 0
for i in range ( 0 ,l) :
sm = sm + ( int )(num[i])
if (sm % 9 ! = 0 ) :
return ( "No" )
return ( "Yes" )
num = "92567812197966231384"
print (divisibleBy36(num))
|
C#
using System;
class GFG {
static bool divisibleBy36(String num)
{
int l = num.Length;
if (l == 0)
return false ;
if (l == 1 && num[0] != '0' )
return false ;
int two_digit_num = (num[l-2] - '0' ) * 10
+ (num[l-1] - '0' ) ;
if (two_digit_num % 4 != 0)
return false ;
int sum = 0;
for ( int i = 0; i < l; i++)
sum += (num[i] - '0' );
if (sum % 9 != 0)
return false ;
return true ;
}
public static void Main ()
{
String num = "92567812197966231384" ;
if (divisibleBy36(num))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function divisibleBy36(num)
{
let l = num.length;
if (l == 0)
return "No" ;
if (l == 1 && num[0] != '0' )
return "No" ;
let two_digit_num = (num[l - 2] - '0' ) * 10 +
(num[l - 1] - '0' ) ;
if (two_digit_num%4 != 0)
return "No" ;
let sum = 0;
for (let i = 0; i < l; i++)
sum += (num[i] - '0' );
if (sum % 9 != 0)
return "No" ;
return "Yes" ;
}
let num = "92567812197966231384" ;
document.write(divisibleBy36(num));
</script>
|
PHP
<?php
function divisibleBy36( $num )
{
$l = strlen ( $num );
if ( $l == 0)
return "No" ;
if ( $l == 1 && $num [0] != '0' )
return "No" ;
$two_digit_num = ( $num [ $l - 2] - '0' ) * 10 +
( $num [ $l - 1] - '0' ) ;
if ( $two_digit_num %4 != 0)
return "No" ;
$sum = 0;
for ( $i = 0; $i < $l ; $i ++)
$sum += ( $num [ $i ] - '0' );
if ( $sum % 9 != 0)
return "No" ;
return "Yes" ;
}
$num = "92567812197966231384" ;
echo (divisibleBy36( $num ));
?>
|
Time Complexity: O(n)
Auxiliary Space: O(1) because constant space for variables is being used
Method 2: Checking given number is divisible by 36 or not by using the modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main()
{
long long int n = 72;
if (n % 36 == 0)
{
cout << "Yes" ;
}
else
{
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
long n = 72 ;
if (n % 36 == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
n = 72
if int (n) % 36 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG
{
static public void Main ()
{
long n = 72;
if (n % 36 == 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
n = 72
if (n % 36 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num = 72;
if ( $num % 36 == 0) {
echo "Yes" ;
}
else {
echo "No" ;
}
?>
|
Time complexity: O(1) as it is doing constant operations
Auxiliary space: O(1)
This article is contributed by Ayush Jauhari.
Approach 3:
Approach:
We can convert the number to a string and use string slicing to check if the last two digits are divisible by 4. Then we can loop through the digits of the number using a for loop and calculate their sum using the built-in sum() function. Finally, we can check if the digit sum is divisible by 9.
- Check if the last two digits of the number are divisible by 4 using the modulo operator.
- Convert the number to a string and loop through its digits using a for loop.
- Convert each digit to an integer using the int() function and calculate their sum using the built-in sum() function.
- Check if the digit sum is divisible by 9 using the modulo operator.
- Return True if both conditions are met, else False.
C++
#include <iostream>
#include <string>
using namespace std;
bool isDivisibleBy36( int n) {
if (stoi(to_string(n).substr(to_string(n).length() - 2)) % 4 != 0) {
return false ;
}
int digitSum = 0;
string numStr = to_string(n);
for ( char digit : numStr) {
digitSum += digit - '0' ;
}
return digitSum % 9 == 0;
}
int main() {
long long n = 92567812197966231384;
if (isDivisibleBy36(n)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
|
Java
import java.math.BigInteger;
public class Main {
public static boolean isDivisibleBy36(BigInteger n) {
if (!n.remainder(BigInteger.valueOf( 4 )).equals(BigInteger.ZERO)) {
return false ;
}
BigInteger digitSum = BigInteger.ZERO;
String numStr = n.toString();
for ( char digit : numStr.toCharArray()) {
digitSum = digitSum.add(BigInteger.valueOf(digit - '0' ));
}
return digitSum.remainder(BigInteger.valueOf( 9 )).equals(BigInteger.ZERO);
}
public static void main(String[] args) {
BigInteger n = new BigInteger( "92567812197966231384" );
if (isDivisibleBy36(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
import time
def is_divisible_by_36_2(n):
if int ( str (n)[ - 2 :]) % 4 ! = 0 :
return False
digit_sum = sum ( int (digit) for digit in str (n))
return digit_sum % 9 = = 0
print (is_divisible_by_36_2( 92567812197966231384 ))
|
C#
using System;
class Program
{
static bool IsDivisibleBy36( string n)
{
if ( int .Parse(n.Substring(n.Length - 2)) % 4 != 0)
{
return false ;
}
int digitSum = 0;
foreach ( char digitChar in n)
{
int digit = int .Parse(digitChar.ToString());
digitSum += digit;
}
return digitSum % 9 == 0;
}
static void Main()
{
string number = "92567812197966231384" ;
bool result = IsDivisibleBy36(number);
Console.WriteLine(result);
}
}
|
Javascript
function isDivisibleBy36(n) {
if (parseInt(n.toString().slice(-2)) % 4 !== 0) {
return false ;
}
const digitSum = n
.toString()
.split( '' )
.map(Number)
.reduce((acc, digit) => acc + digit, 0);
return digitSum % 9 === 0;
}
console.log(isDivisibleBy36(BigInt( "92567812197966231384" )));
|
The time complexity of this approach is also O(log(n)), as we loop through the digits of the number.
The space complexity is O(log(n)), as we convert the number to a string.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...