Check if a large number is divisible by 11 or not
Last Updated :
11 Nov, 2023
Given a number, the task is to check if the number is divisible by 11 or not. The input number may be large and it may not be possible to store it even if we use long long int.
Examples:
Input : n = 76945
Output : Yes
Input : n = 1234567589333892
Output : Yes
Input : n = 363588395960667043875487
Output : No
Since input number may be very large, we cannot use n % 11 to check if a number is divisible by 11 or not, especially in languages like C/C++. The idea is based on following fact.
A number is divisible by 11 if difference of following two is divisible by 11.
- Sum of digits at odd places.
- Sum of digits at even places.
Illustration:
For example, let us consider 76945
Sum of digits at odd places : 7 + 9 + 5
Sum of digits at even places : 6 + 4
Difference of two sums = 21 - 10 = 11
Since difference is divisible by 11, the
number 7945 is divisible by 11.
How does this work?
Let us consider 7694, we can write it as
7694 = 7*1000 + 6*100 + 9*10 + 4
The proof is based on below observation:
Remainder of 10i divided by 11 is 1 if i is even
Remainder of 10i divided by 11 is -1 if i is odd
So the powers of 10 only result in values either 1
or -1.
Remainder of "7*1000 + 6*100 + 9*10 + 4"
divided by 11 can be written as :
7*(-1) + 6*1 + 9*(-1) + 4*1
The above expression is basically difference
between sum of even digits and odd digits.
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
int check(string str)
{
int n = str.length();
int oddDigSum = 0, evenDigSum = 0;
for ( int i=0; i<n; i++)
{
if (i%2 == 0)
oddDigSum += (str[i]- '0' );
else
evenDigSum += (str[i]- '0' );
}
return ((oddDigSum - evenDigSum) % 11 == 0);
}
int main()
{
string str = "76945" ;
check(str)? cout << "Yes" : cout << "No " ;
return 0;
}
|
Java
import java.io.*;
class IsDivisible
{
static boolean check(String str)
{
int n = str.length();
int oddDigSum = 0 , evenDigSum = 0 ;
for ( int i= 0 ; i<n; i++)
{
if (i% 2 == 0 )
oddDigSum += (str.charAt(i)- '0' );
else
evenDigSum += (str.charAt(i)- '0' );
}
return ((oddDigSum - evenDigSum) % 11 == 0 );
}
public static void main (String[] args)
{
String str = "76945" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(st) :
n = len (st)
oddDigSum = 0
evenDigSum = 0
for i in range ( 0 ,n) :
if (i % 2 = = 0 ) :
oddDigSum = oddDigSum + (( int )(st[i]))
else :
evenDigSum = evenDigSum + (( int )(st[i]))
return ((oddDigSum - evenDigSum) % 11 = = 0 )
st = "76945"
if (check(st)) :
print ( "Yes" )
else :
print ( "No " )
|
C#
using System;
class GFG
{
static bool check( string str)
{
int n = str.Length;
int oddDigSum = 0, evenDigSum = 0;
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0)
oddDigSum += (str[i] - '0' );
else
evenDigSum += (str[i] - '0' );
}
return ((oddDigSum - evenDigSum)
% 11 == 0);
}
public static void Main ()
{
String str = "76945" ;
if (check(str))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
function check(str)
{
let n = str.length;
let oddDigSum = 0, evenDigSum = 0;
for (let i = 0; i < n; i++)
{
if (i % 2 == 0)
oddDigSum += (str[i] - '0' );
else
evenDigSum += (str[i] - '0' );
}
return ((oddDigSum - evenDigSum)
% 11 == 0);
}
let str = "76945" ;
if (check(str))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
PHP
<?php
function check( $str )
{
$n = strlen ( $str );
$oddDigSum = 0; $evenDigSum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $i % 2 == 0)
$oddDigSum += ( $str [ $i ] - '0' );
else
$evenDigSum += ( $str [ $i ] - '0' );
}
return (( $oddDigSum - $evenDigSum )
% 11 == 0);
}
$str = "76945" ;
$x = check( $str )? "Yes" : "No " ;
echo ( $x );
?>
|
Time Complexity: O(n), where n is the given number.
Auxiliary Space: O(1), as we are not using any extra space.
Method: Checking given number is divisible by 11 or not by using the modulo division operator “%”.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long n = 1234567589333892;
if (n % 11 == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
Long n = Long.parseUnsignedLong( "1234567589333892" );
if (n % 11 == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
n = 1234567589333892
if int (n) % 11 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
public static void Main( string [] args)
{
long n = 1234567589333892;
if (n % 11 == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
let n = 1234567589333892
if (n % 11 == 0)
console.log( "Yes" )
else
console.log( "No" )
|
PHP
<?php
$num = 1234567589333892;
if ( $num % 11 == 0) {
echo "Yes" ;
}
else {
echo "No" ;
}
?>
|
Time Complexity: O(1) because it is performing constant operations
Auxiliary Space: O(1)
Method: Checking given number is divisible by 11 or not using modulo division.
C++
#include <iostream>
using namespace std;
int main()
{
int num = 76945;
if (num % 11 == 0) {
cout << " divisible" ;
}
else {
cout << " not divisible" ;
}
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int num = 76945 ;
if (num % 11 == 0 ) {
System.out.println( " divisible" );
}
else {
System.out.println( " not divisible" );
}
}
}
|
Python3
n = 76945
if (n % 11 = = 0 ):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static public void Main()
{
double num = 76945;
if (num % 11 == 0) {
Console.Write( " divisible" );
}
else {
Console.Write( " not divisible" );
}
}
|
Javascript
<script>
var n = 76945
if (n % 11 == 0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num = 76945;
if ( $num % 11 == 0)
echo " divisible" ;
else
echo "not divisible" ;
?>
|
Time Complexity : O(1)
Space Complexity : O(1)
Method 4:
1. Initialize two variables: alternating_sum to store the alternating sum of the digits and multiplier to keep track of whether to add or subtract each digit. Set alternating_sum to 0 and multiplier to 1.
2. Use a while loop to iterate over the digits of the number num. The loop will continue as long as num is greater than 0.
a. Get the last digit of the number by using the modulo operator (%) with 10. Store this digit in a temporary variable.
b. Update the alternating_sum by adding (or subtracting) the last digit, multiplied by multiplier.
c. Change the value of multiplier to its opposite by multiplying it by -1.
d. Remove the last digit of the number by using integer division (//) with 10.
3. After the loop, check if the alternating_sum is divisible by 11 by using the modulo operator (%) with 11. If it is, return True, otherwise return False.
C++
#include <iostream>
bool is_divisible_by_11( int num) {
int alternating_sum = 0;
int multiplier = 1;
while (num > 0) {
alternating_sum += multiplier * (num % 10);
multiplier *= -1;
num /= 10;
}
return alternating_sum % 11 == 0;
}
int main() {
std::cout << std::boolalpha;
std::cout << is_divisible_by_11(11) << std::endl;
std::cout << is_divisible_by_11(22) << std::endl;
std::cout << is_divisible_by_11(121) << std::endl;
std::cout << is_divisible_by_11(10) << std::endl;
return 0;
}
|
Java
public class GFG {
public static boolean isDivisibleBy11( int num)
{
int alternatingSum = 0 ;
int multiplier = 1 ;
while (num > 0 ) {
alternatingSum += multiplier * (num % 10 );
multiplier *= - 1 ;
num /= 10 ;
}
return alternatingSum % 11 == 0 ;
}
public static void main(String[] args)
{
System.out.println(isDivisibleBy11( 11 ));
System.out.println(isDivisibleBy11( 22 ));
System.out.println(isDivisibleBy11( 121 ));
System.out.println(isDivisibleBy11( 10 ));
}
}
|
Python3
def is_divisible_by_11(num):
alternating_sum = 0
multiplier = 1
while num > 0 :
alternating_sum + = multiplier * (num % 10 )
multiplier * = - 1
num / / = 10
return alternating_sum % 11 = = 0
print (is_divisible_by_11( 11 ))
print (is_divisible_by_11( 22 ))
print (is_divisible_by_11( 121 ))
print (is_divisible_by_11( 10 ))
|
C#
using System;
class Program {
static bool IsDivisibleBy11( int num)
{
int alternatingSum = 0;
int multiplier = 1;
while (num > 0) {
alternatingSum += multiplier * (num % 10);
multiplier *= -1;
num /= 10;
}
return alternatingSum % 11 == 0;
}
static void Main()
{
Console.WriteLine(IsDivisibleBy11(11));
Console.WriteLine(IsDivisibleBy11(22));
Console.WriteLine(IsDivisibleBy11(121));
Console.WriteLine(IsDivisibleBy11(10));
}
}
|
Javascript
function is_divisible_by_11(num)
{
let alternating_sum = 0;
let multiplier = 1;
while (num > 0)
{
alternating_sum += multiplier * (num % 10);
multiplier *= -1;
num = Math.floor(num / 10);
}
return alternating_sum % 11 == 0;
}
console.log(is_divisible_by_11(11));
console.log(is_divisible_by_11(22));
console.log(is_divisible_by_11(121));
console.log(is_divisible_by_11(10));
|
Output
True
True
True
False
Time complexity: O(log(n))
Auxiliary space: O(1)
Method: Using string manipulation
- The input number is converted to a string, and then the digits are processed one by one in reverse order using a loop.
- The alternating sum is computed by adding each digit to the sum with the appropriate sign (+ or -) based on its position.
- Finally, the alternating sum is checked for divisibility by 11 using the modulo operator (%).
C++
#include <iostream>
#include <string>
bool isDivisibleBy11( const std::string &number) {
int alternatingSum = 0;
bool subtract = true ;
for ( char digit : number) {
int digitValue = digit - '0' ;
if (subtract) {
alternatingSum -= digitValue;
} else {
alternatingSum += digitValue;
}
subtract = !subtract;
}
return (alternatingSum % 11 == 0);
}
int main() {
std::string st = "76945" ;
if (isDivisibleBy11(st)) {
std::cout << "Yes" << std::endl;
} else {
std::cout << "No" << std::endl;
}
return 0;
}
|
Java
public class Main {
public static boolean isDivisibleBy11(String number) {
int alternatingSum = 0 ;
boolean subtract = true ;
for ( char digit : number.toCharArray()) {
int digitValue = digit - '0' ;
if (subtract) {
alternatingSum -= digitValue;
} else {
alternatingSum += digitValue;
}
subtract = !subtract;
}
return (alternatingSum % 11 == 0 );
}
public static void main(String[] args) {
String st = "76945" ;
if (isDivisibleBy11(st)) {
System.out.println( "Yes" );
} else {
System.out.println( "No" );
}
}
}
|
Python3
def is_divisible_by_11(number):
alternating_sum = 0
for i, digit in enumerate ( reversed ( str (number))):
alternating_sum + = ( - 1 ) * * i * int (digit)
return alternating_sum % 11 = = 0
st = "76945"
if (is_divisible_by_11(st)) :
print ( "Yes" )
else :
print ( "No " )
|
C#
using System;
class Program
{
static bool IsDivisibleBy11( string number)
{
int alternatingSum = 0;
bool subtract = true ;
foreach ( char digit in number)
{
int digitValue = digit - '0' ;
if (subtract)
{
alternatingSum -= digitValue;
}
else
{
alternatingSum += digitValue;
}
subtract = !subtract;
}
return (alternatingSum % 11 == 0);
}
static void Main()
{
string st = "76945" ;
if (IsDivisibleBy11(st))
{
Console.WriteLine( "Yes" );
}
else
{
Console.WriteLine( "No" );
}
}
}
|
Javascript
function isDivisibleBy11(number) {
let alternatingSum = 0;
let subtract = true ;
for (let i = 0; i < number.length; i++) {
let digitValue = parseInt(number[i], 10);
if (subtract) {
alternatingSum -= digitValue;
} else {
alternatingSum += digitValue;
}
subtract = !subtract;
}
return alternatingSum % 11 === 0;
}
const st = "76945" ;
if (isDivisibleBy11(st)) {
console.log( "Yes, the number is divisible by 11" );
} else {
console.log( "No, the number is not divisible by 11" );
}
|
Time complexity:
The time complexity of the function is O(n), where n is the number of digits in the input number.
This is because the function processes each digit of the input number once in the loop.
Auxiliary space:
The auxiliary space complexity of the function is O(1), because it uses a constant amount of extra memory to store the alternating sum and loop variables.
The amount of memory used does not depend on the size of the input number.
This article is contributed by DANISH_RAZA .
Share your thoughts in the comments
Please Login to comment...