Check if a large number is divisible by 4 or not
Last Updated :
13 Sep, 2023
Given a number, the task is to check if a number is divisible by 4 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 = 1124
Output : Yes
Input : n = 1234567589333862
Output : No
Input : n = 363588395960667043875487
Output : No
Since input number may be very large, we cannot use n % 4 to check if a number is divisible by 4 or not, especially in languages like C/C++. The idea is based on following fact.
A number is divisible by 4 if number formed by last two digits of it is divisible by 4.
Illustration:
For example, let us consider 76952
Number formed by last two digits = 52
Since 52 is divisible by 4, answer is YES.
How does this work?
Let us consider 76952, we can write it as
76952 = 7*10000 + 6*1000 + 9*100 + 5*10 + 2
The proof is based on below observation:
Remainder of 10i divided by 4 is 0 if i greater
than or equal to two. Note than 100, 1000,
... etc lead to remainder 0 when divided by 4.
So remainder of "7*10000 + 6*1000 + 9*100 +
5*10 + 2" divided by 4 is equivalent to remainder
of following :
0 + 0 + 0 + 5*10 + 2 = 52
Therefore we can say that the whole number is
divisible by 4 if 52 is divisible by 4.
Below is implementation of above idea :
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string str)
{
int n = str.length();
if (n == 0)
return false ;
if (n == 1)
return ((str[0] - '0' ) % 4 == 0);
int last = str[n - 1] - '0' ;
int second_last = str[n - 2] - '0' ;
return ((second_last * 10 + last) % 4 == 0);
}
int main()
{
string str = "76952" ;
check(str) ? cout << "Yes" : cout << "No " ;
return 0;
}
|
Java
import java.util.*;
class IsDivisible
{
static boolean check(String str)
{
int n = str.length();
if (n == 0 )
return false ;
if (n == 1 )
return ((str.charAt( 0 ) - '0' ) % 4 == 0 );
int last = str.charAt(n - 1 ) - '0' ;
int second_last = str.charAt(n - 2 ) - '0' ;
return ((second_last * 10 + last) % 4 == 0 );
}
public static void main(String[] args)
{
String str = "76952" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(st):
n = len (st)
if (n = = 0 ):
return False
if (n = = 1 ):
return ((st[ 0 ] - '0' ) % 4 = = 0 )
last = ( int )(st[n - 1 ])
second_last = ( int )(st[n - 2 ])
return ((second_last * 10 + last) % 4 = = 0 )
st = "76952"
if (check(st)):
print ( "Yes" )
else :
print ( "No " )
|
C#
using System;
class GFG
{
static bool check(String str)
{
int n = str.Length;
if (n == 0)
return false ;
if (n == 1)
return ((str[0] - '0' ) % 4 == 0);
int last = str[n - 1] - '0' ;
int second_last = str[n - 2] - '0' ;
return ((second_last * 10 + last) % 4 == 0);
}
public static void Main()
{
String str = "76952" ;
if (check(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
function check(str)
{
var n = str.length;
if ( n == 0)
{
return false ;
}
if ( n == 1)
{
return ((str[0] - '0' ) % 4 == 0);
}
var lastNumber = str[n-1] - '0' ;
var lastSecondNUmber = str[n-2] - '0' ;
return ((lastSecondNUmber * 10 + lastNumber) % 4 == 0);
}
var str= "76952" ;
if (check(str)){
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
PHP
<?php
function check( $str )
{
$n = strlen ( $str );
if ( $n == 0)
return false;
if ( $n == 1)
return (( $str [0] - '0' ) % 4 == 0);
$last = $str [ $n - 1] - '0' ;
$second_last = $str [ $n - 2] - '0' ;
return (( $second_last * 10 + $last ) % 4 == 0);
}
$str = "76952" ;
$x = check( $str )? "Yes" : "No" ;
echo ( $x );
?>
|
Time Complexity: O(1), as we are not using any loops for traversing.
Auxiliary Space: O(1), as we are not using any extra space.
Method 2: Checking given number is divisible by 4 or not by using the modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main()
{
long long int n = 1234567589333862;
if (n % 4 == 0)
{
cout << "Yes" ;
}
else
{
cout << "No" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args) {
long n=123456758933l;
if (n % 4 == 0 )
{
System.out.println( "Yes" );
}
else
{
System.out.println( "No" );
}
}
}
|
Python3
n = 1234567589333862
if int (n) % 4 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG{
static public void Main (){
long n=1234567589333862;
if (n % 4 == 0)
{
Console.Write( "Yes" );
}
else
{
Console.Write( "No" );
}
}
}
|
Javascript
<script>
var n = 1234567589333862
if (n % 4 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num = 1234567589333862;
if ( $num % 4 == 0) {
echo "true" ;
}
else {
echo "false" ;
}
?>
|
Time Complexity: O(1), as we are not using any loops for traversing.
Auxiliary Space: O(1), as we are not using any extra space.
Method 3: Use of inbuilt function Atoi() in C++.
The atoi() function in C++ takes a string (which represents an integer) as an argument and returns its value of type int. So basically the function is used to convert a string argument to an integer.
Syntax:
int atoi(const char str)
Parameters: The function accepts one parameter str which refers to the string argument that is needed to be converted into its integer equivalent.
Return Value: If str is a valid input, then the function returns the equivalent integer number for the passed string number. If no valid conversion takes place, then the function returns zero.
Implementation
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
char str[] = "76952" ;
int n = ( sizeof (str) / sizeof ( char ))-1;
char ch[2];
if (n >= 2) {
ch[0] = str[n - 2];
ch[1] = str[n - 1];
}
else if (n == 1) {
ch[0] = 0;
ch[1] = str[0];
}
int x;
x = atoi (ch);
if (!(x % 4)) {
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static void main(String[] args)
{
String str = "76952" ;
int n = str.length();
char [] ch = new char [ 2 ];
if (n >= 2 ) {
ch[ 0 ] = str.charAt(n - 2 );
ch[ 1 ] = str.charAt(n - 1 );
}
else if (n == 1 ) {
ch[ 0 ] = '0' ;
ch[ 1 ] = str.charAt( 0 );
}
int x = (ch[ 0 ] - '0' ) * 10 + (ch[ 1 ] - '0' );
if ((x % 4 ) == 0 ) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
|
Python3
str = "76952" ;
n = len ( str );
ch = [" ", " "]
if (n > = 2 ):
ch[ 0 ] = str [n - 2 ]
ch[ 1 ] = str [n - 1 ]
elif (n = = 1 ):
ch[ 0 ] = '0' ;
ch[ 1 ] = str [ 0 ];
x = int ("".join(ch));
if (x % 4 = = 0 ):
print ( "YES" );
else :
print ( "NO" );
|
C#
using System;
class GFG {
public static void Main( string [] args)
{
string str = "76952" ;
int n = str.Length;
char [] ch = new char [2];
if (n >= 2) {
ch[0] = str[n - 2];
ch[1] = str[n - 1];
}
else if (n == 1) {
ch[0] = '0' ;
ch[1] = str[0];
}
int x = (ch[0] - '0' ) * 10 + (ch[1] - '0' );
if ((x % 4) == 0) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
|
Javascript
let str = "76952" ;
let n = str.length;
let ch = new Array(2);
if (n >= 2) {
ch[0] = str.charAt(n - 2);
ch[1] = str.charAt(n - 1);
}
else if (n == 1) {
ch[0] = '0' ;
ch[1] = str.charAt(0);
}
let x;
x = parseInt(ch.join( "" ));
if (x % 4 == 0) {
console.log( "YES" );
}
else {
console.log( "NO" );
}
|
Time Complexity: O(1), as we are not using any loops for traversing.
Auxiliary Space: O(1), as we are not using any extra space.
Method 4: (Using substring function)
- Use substring function to get the last two characters of the string.
- Convert the string to integer
- Check if it is divisible by 4 or not, using (number%4 == 0).
This approach is contributed by Abhijeet Kumar.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool check(string str)
{
int n = str.length();
if (n == 0)
return false ;
if (n == 1)
return ((stoi(str)) % 4 == 0);
str = str.substr(n - 2, 2);
return ((stoi(str)) % 4 == 0);
}
int main()
{
string str = "76952" ;
check(str) ? cout << "Yes" : cout << "No " ;
return 0;
}
|
Java
import java.util.*;
class IsDivisible {
static boolean check(String str)
{
int n = str.length();
if (n == 0 )
return false ;
if (n == 1 )
return ((Integer.parseInt(str)) % 4 == 0 );
str = str.substring(n - 2 );
return ((Integer.parseInt(str)) % 4 == 0 );
}
public static void main(String[] args)
{
String str = "76952" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(st):
n = len (st)
if (n = = 0 ):
return False
if (n = = 1 ):
return ( int (st) % 4 = = 0 )
st = st[n - 2 :]
return ( int (st) % 4 = = 0 )
st = "76952"
if (check(st)):
print ( "Yes" )
else :
print ( "No " )
|
C#
using System;
class GFG
{
static bool check(String str)
{
int n = str.Length;
if (n == 0)
return false ;
if (n == 1)
return ( int .Parse(str) % 4 == 0);
str = str.Substring(n-2);
return ( int .Parse(str) % 4 == 0);
}
public static void Main()
{
String str = "76952" ;
if (check(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
function check(str)
{
var n = str.length;
if ( n == 0)
{
return false ;
}
if ( n == 1)
{
return (parseInt(str) % 4 == 0);
}
str = str.substring(n-2);
return (parseInt(str) % 4 == 0);
}
var str= "76952" ;
if (check(str)){
console.log( "Yes" );
}
else {
console.log( "No" );
}
|
Time Complexity: (1), substring function takes O(n) time, where n is the length of the substring and as here n is equal to 2 thus the time complexity is constant.
Auxiliary Space: O(1), As constant extra space is used.
This article is contributed by Aarti_Rathi and DANISH_RAZA .
Share your thoughts in the comments
Please Login to comment...