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++ implementation to check divisibility by 36 #include <bits/stdc++.h> using namespace std;
// Function to check whether a number // is divisible by 36 or not string divisibleBy36(string num) { int l = num.length();
// null number cannot
// be divisible by 36
if (l == 0)
return "No" ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num[0] != '0' )
return "No" ;
// number formed by the last 2 digits
int two_digit_num = (num[l-2] - '0' )*10 +
(num[l-1] - '0' ) ;
// if number is not divisible by 4
if (two_digit_num%4 != 0)
return "No" ;
// number is divisible by 4 calculate
// sum of digits
int sum = 0;
for ( int i=0; i<l; i++)
sum += (num[i] - '0' );
// sum of digits is not divisible by 9
if (sum%9 != 0)
return "No" ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return "Yes" ;
} // Driver program int main()
{ string num = "92567812197966231384" ;
cout << divisibleBy36(num);
return 0;
} |
// Java program to find if a number is // divisible by 36 or not import java.io.*;
class IsDivisible
{ // Function to check whether a number
// is divisible by 36 or not
static boolean divisibleBy36(String num)
{
int l = num.length();
// null number cannot
// be divisible by 36
if (l == 0 )
return false ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num.charAt( 0 ) != '0' )
return false ;
// number formed by the last 2 digits
int two_digit_num = (num.charAt(l- 2 ) - '0' )* 10 +
(num.charAt(l- 1 ) - '0' ) ;
// if number is not divisible by 4
if (two_digit_num% 4 != 0 )
return false ;
// number is divisible by 4 calculate
// sum of digits
int sum = 0 ;
for ( int i= 0 ; i<l; i++)
sum += (num.charAt(i) - '0' );
// sum of digits is not divisible by 9
if (sum% 9 != 0 )
return false ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return true ;
}
// main function
public static void main (String[] args)
{
String num = "92567812197966231384" ;
if (divisibleBy36(num))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
} |
# Python 3 implementation to # check divisibility by 36 # Function to check whether a # number is divisible by # 36 or not def divisibleBy36(num) :
l = len (num)
# null number cannot
# be divisible by 36
if (l = = 0 ) :
return ( "No" )
# single digit number other
# than 0 is not divisible
# by 36
if (l = = 1 and num[ 0 ] ! = '0' ) :
return ( "No" )
# number formed by the
# last 2 digits
two_digit_num = ((( int )(num[l - 2 ])) *
10 + ( int )(num[l - 1 ]))
# if number is not
# divisible by 4
if (two_digit_num % 4 ! = 0 ) :
return "No"
# number is divisible
# by 4 calculate sum
# of digits
sm = 0
for i in range ( 0 ,l) :
sm = sm + ( int )(num[i])
# sum of digits is not
# divisible by 9
if (sm % 9 ! = 0 ) :
return ( "No" )
# Number is divisible
# by 4 and 9 hence,
# number is divisible
# by 36
return ( "Yes" )
# Driver program num = "92567812197966231384"
print (divisibleBy36(num))
# This code is contributed by Nikita Tiwari. |
// C# program to find if a number is // divisible by 36 or not using System;
class GFG {
// Function to check whether
// a number is divisible by
// 36 or not
static bool divisibleBy36(String num)
{
int l = num.Length;
// null number cannot
// be divisible by 36
if (l == 0)
return false ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num[0] != '0' )
return false ;
// number formed by the last
// 2 digits
int two_digit_num = (num[l-2] - '0' ) * 10
+ (num[l-1] - '0' ) ;
// if number is not divisible by 4
if (two_digit_num % 4 != 0)
return false ;
// number is divisible by 4 calculate
// sum of digits
int sum = 0;
for ( int i = 0; i < l; i++)
sum += (num[i] - '0' );
// sum of digits is not divisible by 9
if (sum % 9 != 0)
return false ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return true ;
}
// main function
public static void Main ()
{
String num = "92567812197966231384" ;
if (divisibleBy36(num))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
} // This code is contributed by parashar. |
<script> // Javascript implementation to // check divisibility by 36 // Function to check whether a number // is divisible by 36 or not function divisibleBy36(num)
{ let l = num.length;
// null number cannot
// be divisible by 36
if (l == 0)
return "No" ;
// single digit number other than
// 0 is not divisible by 36
if (l == 1 && num[0] != '0' )
return "No" ;
// number formed by the
// last 2 digits
let two_digit_num = (num[l - 2] - '0' ) * 10 +
(num[l - 1] - '0' ) ;
// if number is not
// divisible by 4
if (two_digit_num%4 != 0)
return "No" ;
// number is divisible by 4
// calculate sum of digits
let sum = 0;
for (let i = 0; i < l; i++)
sum += (num[i] - '0' );
// sum of digits is not
// divisible by 9
if (sum % 9 != 0)
return "No" ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return "Yes" ;
} // Driver Code let num = "92567812197966231384" ;
document.write(divisibleBy36(num)); // This code is contributed by _saurabh_jaiswal. </script> |
<?php // PHP implementation to // check divisibility by 36 // Function to check whether a number // is divisible by 36 or not function divisibleBy36( $num )
{ $l = strlen ( $num );
// null number cannot
// be divisible by 36
if ( $l == 0)
return "No" ;
// single digit number other than
// 0 is not divisible by 36
if ( $l == 1 && $num [0] != '0' )
return "No" ;
// number formed by the
// last 2 digits
$two_digit_num = ( $num [ $l - 2] - '0' ) * 10 +
( $num [ $l - 1] - '0' ) ;
// if number is not
// divisible by 4
if ( $two_digit_num %4 != 0)
return "No" ;
// number is divisible by 4
// calculate sum of digits
$sum = 0;
for ( $i = 0; $i < $l ; $i ++)
$sum += ( $num [ $i ] - '0' );
// sum of digits is not
// divisible by 9
if ( $sum % 9 != 0)
return "No" ;
// number is divisible by 4 and 9
// hence, number is divisible by 36
return "Yes" ;
} // Driver Code $num = "92567812197966231384" ;
echo (divisibleBy36( $num ));
// This code is contributed by Ajit. ?> |
Yes
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 “%”.
#include <iostream> using namespace std;
int main()
{ //input
long long int n = 72;
// finding given number is divisible by 36 or not
if (n % 36 == 0)
{
cout << "Yes" ;
}
else
{
cout << "No" ;
}
return 0;
} // This code is contributed by satwik4409. |
/*package whatever //do not write package name here */ import java.io.*;
class GFG {
public static void main (String[] args) {
// Java code for the above approach
// To check whether the given number is divisible by 36 or not
//input
long n = 72 ;
// finding given number is divisible by 36 or not
if (n % 36 == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
} // This code is contributed by laxmigangarajula03 |
# Python code # To check whether the given number is divisible by 36 or not #input n = 72
# the above input can also be given as n=input() -> taking input from user # finding given number is divisible by 36 or not if int (n) % 36 = = 0 :
print ( "Yes" )
else :
print ( "No" )
# this code is contributed by gangarajula laxmi
|
using System;
public class GFG
{ static public void Main ()
{
// C# code for the above approach
// To check whether the given number is divisible by 36 or not
//input
long n = 72;
// finding given number is divisible by 36 or not
if (n % 36 == 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
} // this code is contributed by ksrikanth0498 |
<script> // JavaScript code for the above approach
// To check whether the given number is divisible by 36 or not
//input
n = 72
// finding given number is divisible by 36 or not
if (n % 36 == 0)
document.write( "Yes" )
else
document.write( "No" )
// This code is contributed by Potta Lokesh
</script>
|
<?php $num = 72;
// checking if the given number is divisible by 36 or
// not using modulo division operator if the output of
// num%36 is equal to 0 then given number is divisible
// by 36 otherwise not divisible by 36
if ( $num % 36 == 0) {
echo "Yes" ;
}
else {
echo "No" ;
}
?> |
Yes
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.
#include <iostream> #include <string> using namespace std;
bool isDivisibleBy36( int n) {
// Check if the last two digits of the number are divisible by 4
if (stoi(to_string(n).substr(to_string(n).length() - 2)) % 4 != 0) {
return false ;
}
int digitSum = 0;
string numStr = to_string(n);
// Calculate the sum of all the digits in the number
for ( char digit : numStr) {
digitSum += digit - '0' ; // Convert char to int and add to the sum
}
// Check if the sum of digits is divisible by 9
return digitSum % 9 == 0;
} int main() {
long long n = 92567812197966231384;
// Function Call
if (isDivisibleBy36(n)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
} |
import java.math.BigInteger;
public class Main {
// Function to check if a number is divisible by 36
public static boolean isDivisibleBy36(BigInteger n) {
// Check if the last two digits of the number are divisible by 4
if (!n.remainder(BigInteger.valueOf( 4 )).equals(BigInteger.ZERO)) {
return false ;
}
BigInteger digitSum = BigInteger.ZERO;
String numStr = n.toString();
// Calculate the sum of all the digits in the number
for ( char digit : numStr.toCharArray()) {
digitSum = digitSum.add(BigInteger.valueOf(digit - '0' ));
}
// Check if the sum of digits is divisible by 9
return digitSum.remainder(BigInteger.valueOf( 9 )).equals(BigInteger.ZERO);
}
public static void main(String[] args) {
BigInteger n = new BigInteger( "92567812197966231384" );
// Function Call
if (isDivisibleBy36(n)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
} |
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 ))
|
using System;
class Program
{ static bool IsDivisibleBy36( string n)
{
// Check if the last two digits of n are divisible by 4
if ( int .Parse(n.Substring(n.Length - 2)) % 4 != 0)
{
return false ;
}
// Convert n to an array of digits and calculate the sum
int digitSum = 0;
foreach ( char digitChar in n)
{
int digit = int .Parse(digitChar.ToString());
digitSum += digit;
}
// Check if the sum of digits is divisible by 9
return digitSum % 9 == 0;
}
static void Main()
{
// Test the function with a large number
string number = "92567812197966231384" ;
bool result = IsDivisibleBy36(number);
Console.WriteLine(result); // Should output True
}
} |
function isDivisibleBy36(n) {
// Check if the last two digits of n are divisible by 4
if (parseInt(n.toString().slice(-2)) % 4 !== 0) {
return false ;
}
// Convert n to a string and split it into an array of digits,
// then convert each digit back to a number
const digitSum = n
.toString()
.split( '' )
.map(Number)
// Calculate the sum of all the digits in the number
.reduce((acc, digit) => acc + digit, 0);
// Check if the sum of digits is divisible by 9
return digitSum % 9 === 0;
} // Test the function with a large number console.log(isDivisibleBy36(BigInt( "92567812197966231384" ))); // Should output true
|
True
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.