Divisibility by 12 for a large number
Last Updated :
30 Jan, 2023
Given a large number, the task is to check whether the number is divisible by 12 or not.
Examples :
Input : 12244824607284961224
Output : Yes
Input : 92387493287593874594898678979792
Output : No
Method 1: This is a very simple approach. if a number is divisible by 4 and 3 then the number is divisible by 12.
Point 1. If the last two digits of the number are divisible by 4 then the number is divisible by 4. Please see divisibility by 4 for large numbers for details.
Point 2. if the sum of all digits of a number is divided by 3 then the number is divisible by 3. Please see divisibility by 3 for large numbers for details.
C++
#include <iostream>
using namespace std;
bool isDvisibleBy12(string num)
{
if (num.length() >= 3) {
int d1 = ( int )num[num.length() - 1];
if (d1 % 2 != 0)
return (0);
int d2 = ( int )num[num.length() - 2];
int sum = 0;
for ( int i = 0; i < num.length(); i++)
sum += num[i];
return (sum % 3 == 0 && (d2 * 10 + d1) % 4 == 0);
}
else {
int number = stoi(num);
return (number % 12 == 0);
}
}
int main()
{
string num = "12244824607284961224" ;
if (isDvisibleBy12(num))
cout << "Yes" << endl;
else
cout << "No" << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static boolean isDvisibleBy12(String num)
{
if (num.length() >= 3 ) {
int d1 = ( int )num.charAt(num.length() - 1 );
if (d1 % 2 != 0 )
return false ;
int d2 = ( int )num.charAt(num.length() - 2 );
int sum = 0 ;
for ( int i = 0 ; i < num.length(); i++)
sum += num.charAt(i);
return (sum % 3 == 0 &&
(d2 * 10 + d1) % 4 == 0 );
}
else {
int number = Integer.parseInt(num);
return (number % 12 == 0 );
}
}
public static void main (String[] args) {
String num = "12244824607284961224" ;
if (isDvisibleBy12(num))
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
import math
def isDvisibleBy12( num):
if ( len (num) > = 3 ):
d1 = int (num[ len (num) - 1 ]);
if (d1 % 2 ! = 0 ):
return False
d2 = int (num[ len (num) - 2 ])
sum = 0
for i in range ( 0 , len (num) ):
sum + = int (num[i])
return ( sum % 3 = = 0 and
(d2 * 10 + d1) % 4 = = 0 )
else :
number = int (num)
return (number % 12 = = 0 )
num = "12244824607284961224"
if (isDvisibleBy12(num)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG
{
static bool isDvisibleBy12( string num)
{
if (num.Length >= 3) {
int d1 = ( int )num[num.Length - 1];
if (d1 % 2 != 0)
return false ;
int d2 = ( int )num[num.Length - 2];
int sum = 0;
for ( int i = 0; i < num.Length; i++)
sum += num[i];
return (sum % 3 == 0 &&
(d2 * 10 + d1) % 4 == 0);
}
else {
int number = int .Parse(num);
return (number % 12 == 0);
}
}
public static void Main ()
{
String num = "12244824607284961224" ;
if (isDvisibleBy12(num))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function isDvisibleBy12( $num )
{
if ( strlen ( $num ) >= 3)
{
$d1 = (int) $num [ strlen ( $num ) - 1];
if ( $d1 % 2 != 0)
return (0);
$d2 = (int) $num [ strlen ( $num ) - 2];
$sum = 0;
for ( $i = 0; $i < strlen ( $num ); $i ++)
$sum += $num [ $i ];
return ( $sum % 3 == 0 &&
( $d2 * 10 + $d1 ) % 4 == 0);
}
else {
$number = stoi( $num );
return ( $number % 12 == 0);
}
}
$num = "12244824607284961224" ;
if (isDvisibleBy12( $num ))
echo ( "Yes" );
else
echo ( "No" );
?>
|
Javascript
<script>
function isDvisibleBy12(num)
{
if (num.length >= 3)
{
let d1 = num[num.length - 1].charCodeAt();
if (d1 % 2 != 0)
return false ;
let d2 = num[num.length - 2].charCodeAt();
let sum = 0;
for (let i = 0; i < num.length; i++)
sum += num[i].charCodeAt();
return ((sum % 3 == 0) &&
(d2 * 10 + d1) % 4 == 0);
}
else
{
let number = parseInt(num, 10);
document.write(number);
return (number % 12 == 0);
}
}
let num = "12244824607284961224" ;
if (isDvisibleBy12(num))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N), where N is the length of the given string.
Auxiliary Space: O(1)
Method: Checking given number is divisible by 12 or not by using the modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main() {
long long int n = 12244824607;
if (n % 12 == 0){
cout<< "Yes" ;
}
else {
cout<< "No" ;
}
return 0;
}
|
Java
import java.math.BigInteger;
class GFG {
public static void main(String[] args)
{
BigInteger num = new BigInteger( "12244824607284961224" );
if (num.mod( new BigInteger( "12" ))
.equals( new BigInteger( "0" ))) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
n = 12244824607284961224
if int (n) % 12 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static public void Main()
{
double num = 12244824607284961224;
if (num % 12 == 0) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
Javascript
<script>
var n = 12244824607284961224
if (n % 12 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num = 12244824607;
if ( $num % 12 == 0)
echo "Yes" ;
else
echo "No" ;
?>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...