Check if a large number is divisible by 6 or not
Given a number, the task is to check if a number is divisible by 6 or not. The input number may be large and it may not be possible to store even if we use long long int.
Examples:
Input : n = 2112
Output: Yes
Input : n = 1124
Output : No
Input : n = 363588395960667043875487
Output : No
C++
#include <iostream>
using namespace std;
int main() {
long n = 36358839596;
if (n % 6 == 0)
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
long n = 36358839596L;
if (n % 6 == 0 )
System.out.print( "Yes" );
else
System.out.print( "No" );
}
}
|
Python3
n = 363588395960667043875487
if int (n) % 6 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static public void Main()
{
long n = 36358839596;
if (n % 6 == 0)
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
var n = 363588395960667043875487
if (n % 6 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num = 363588395960667043875487;
if ( $num %6 == 0)
echo "Yes" ;
else
echo "No" ;
?>
|
Time complexity: O(1) as it is performing constant operations
Auxiliary Space: O(1) as it is using constant space for variables
Since input number may be very large, we cannot use n % 6 to check if a number is divisible by 6 or not, especially in languages like C/C++. The idea is based on following fact.
A number is divisible by 6 it's divisible by 2 and 3.
a) A number is divisible by 2 if its last digit is divisible by 2.
b) A number is divisible by 3 if sum of digits is divisible by 3.
Below is the implementation based on above steps.
C++
#include<bits/stdc++.h>
using namespace std;
bool check(string str)
{
int n = str.length();
if ((str[n-1]- '0' )%2 != 0)
return false ;
int digitSum = 0;
for ( int i=0; i<n; i++)
digitSum += (str[i]- '0' );
return (digitSum % 3 == 0);
}
int main()
{
string str = "1332" ;
check(str)? cout << "Yes" : cout << "No " ;
return 0;
}
|
Java
import java.io.*;
class IsDivisible
{
static boolean check(String str)
{
int n = str.length();
if ((str.charAt(n- 1 ) - '0' )% 2 != 0 )
return false ;
int digitSum = 0 ;
for ( int i= 0 ; i<n; i++)
digitSum += (str.charAt(i)- '0' );
return (digitSum % 3 == 0 );
}
public static void main (String[] args)
{
String str = "1332" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(st) :
n = len (st)
if ((( int )(st[n - 1 ]) % 2 ) ! = 0 ) :
return False
digitSum = 0
for i in range ( 0 , n) :
digitSum = digitSum + ( int )(st[i])
return (digitSum % 3 = = 0 )
st = "1332"
if (check(st)) :
print ( "Yes" )
else :
print ( "No " )
|
C#
using System;
class GFG {
static bool check(String str)
{
int n = str.Length;
if ((str[n-1] - '0' ) % 2 != 0)
return false ;
int digitSum = 0;
for ( int i = 0; i < n; i++)
digitSum += (str[i] - '0' );
return (digitSum % 3 == 0);
}
public static void Main ()
{
String str = "1332" ;
if (check(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function check( $str )
{
$n = strlen ( $str );
if (( $str [ $n - 1] - '0' ) % 2 != 0)
return false;
$digitSum = 0;
for ( $i = 0; $i < $n ; $i ++)
$digitSum += ( $str [ $i ] - '0' );
return ( $digitSum % 3 == 0);
}
$str = "1332" ;
if (check( $str ))
echo "Yes" ;
else
echo " No " ;
return 0;
?>
|
Javascript
<script>
function check(str)
{
let n = str.length;
if ((str[n-1] - '0' ) % 2 != 0)
return false ;
let digitSum = 0;
for (let i = 0; i < n; i++)
digitSum += (str[i] - '0' );
return (digitSum % 3 == 0);
}
let str = "1332" ;
if (check(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(logN) where N is the given number
Auxiliary Space: O(1) since no extra space is being used
This article is contributed by DANISH_RAZA .
Approach#3: using the fact that it is divisible by 3 and 2 consecutive even numbers
We can check if a number is divisible by 6 by checking if it is divisible by 3 and any 2 consecutive even numbers (i.e., 2 and 4 or 4 and 6, etc.). We can use the modulo operation to find the remainder of the number when divided by 3 and check if it is divisible by any 2 consecutive even numbers.
Algorithm
1. Check if the remainder of the number when divided by 3 is 0.
2. Check if the last digit of the number is even.
3. If the last digit of the number is even, check if the second-last digit of the number is odd.
4. If the second-last digit of the number is odd, print “Yes”, otherwise print “No”.
C++
#include <iostream>
using namespace std;
void check_divisibility_by_6( int n)
{
if (n % 3 == 0 && n % 10 % 2 == 0
&& (n / 10) % 10 % 2 != 0) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
}
int main()
{
int n1 = 2112;
check_divisibility_by_6(n1);
int n2 = 1124;
check_divisibility_by_6(n2);
return 0;
}
|
Java
public class Main {
public static void check_divisibility_by_6( int n) {
if (n % 3 == 0 && n % 10 % 2 == 0 && (n / 10 ) % 10 % 2 != 0 ) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
public static void main(String[] args) {
int n1 = 2112 ;
check_divisibility_by_6(n1);
int n2 = 1124 ;
check_divisibility_by_6(n2);
}
}
|
Python3
def check_divisibility_by_6(n):
if n % 3 = = 0 and n % 10 % 2 = = 0 and (n / / 10 ) % 10 % 2 ! = 0 :
print ( "Yes" )
else :
print ( "No" )
n = 2112
check_divisibility_by_6(n)
n = 1124
check_divisibility_by_6(n)
|
Javascript
function check_divisibility_by_6(n) {
if (n % 3 === 0 && n % 10 % 2 === 0 && Math.floor(n / 10) % 10 % 2 !== 0) {
console.log( "Yes" );
} else {
console.log( "No" );
}
}
let n = 2112;
check_divisibility_by_6(n);
n = 1124;
check_divisibility_by_6(n);
|
C#
using System;
class Program
{
static void CheckDivisibilityBy6( int n)
{
if (n % 3 == 0 && n % 10 % 2 == 0
&& (n / 10) % 10 % 2 != 0)
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
static void Main( string [] args)
{
int n1 = 2112;
CheckDivisibilityBy6(n1);
int n2 = 1124;
CheckDivisibilityBy6(n2);
Console.ReadKey();
}
}
|
Time Complexity: O(log n) where n is the value of the number.
Auxiliary Space: O(1).
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...