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++ program to find if a number is divisible by // 11 or not #include<bits/stdc++.h> using namespace std;
// Function to find that number divisible by 11 or not int check(string str)
{ int n = str.length();
// Compute sum of even and odd digit
// sums
int oddDigSum = 0, evenDigSum = 0;
for ( int i=0; i<n; i++)
{
// When i is even, position of digit is odd
if (i%2 == 0)
oddDigSum += (str[i]- '0' );
else
evenDigSum += (str[i]- '0' );
}
// Check its difference is divisible by 11 or not
return ((oddDigSum - evenDigSum) % 11 == 0);
} // Driver code int main()
{ string str = "76945" ;
check(str)? cout << "Yes" : cout << "No " ;
return 0;
} |
// Java program to find if a number is // divisible by 11 or not import java.io.*;
class IsDivisible
{ // Function to find that number divisible by 11 or not
static boolean check(String str)
{
int n = str.length();
// Compute sum of even and odd digit
// sums
int oddDigSum = 0 , evenDigSum = 0 ;
for ( int i= 0 ; i<n; i++)
{
// When i is even, position of digit is odd
if (i% 2 == 0 )
oddDigSum += (str.charAt(i)- '0' );
else
evenDigSum += (str.charAt(i)- '0' );
}
// Check its difference is divisible by 11 or not
return ((oddDigSum - evenDigSum) % 11 == 0 );
}
// main function
public static void main (String[] args)
{
String str = "76945" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
} |
# Python 3 code program to find if a number # is divisible by 11 or not # Function to find that number divisible by # 11 or not def check(st) :
n = len (st)
# Compute sum of even and odd digit
# sums
oddDigSum = 0
evenDigSum = 0
for i in range ( 0 ,n) :
# When i is even, position of digit is odd
if (i % 2 = = 0 ) :
oddDigSum = oddDigSum + (( int )(st[i]))
else :
evenDigSum = evenDigSum + (( int )(st[i]))
# Check its difference is divisible by 11 or not
return ((oddDigSum - evenDigSum) % 11 = = 0 )
# Driver code st = "76945"
if (check(st)) :
print ( "Yes" )
else :
print ( "No " )
# This code is contributed by Nikita tiwari. |
// C# program to find if a number is // divisible by 11 or not using System;
class GFG
{ // Function to find that number
// divisible by 11 or not
static bool check( string str)
{
int n = str.Length;
// Compute sum of even and odd digit
// sums
int oddDigSum = 0, evenDigSum = 0;
for ( int i = 0; i < n; i++)
{
// When i is even, position of
// digit is odd
if (i % 2 == 0)
oddDigSum += (str[i] - '0' );
else
evenDigSum += (str[i] - '0' );
}
// Check its difference is
// divisible by 11 or not
return ((oddDigSum - evenDigSum)
% 11 == 0);
}
// main function
public static void Main ()
{
String str = "76945" ;
if (check(str))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
} // This code is contributed by vt_m. |
<script> // JavaScript program for the above approach // Function to find that number
// divisible by 11 or not
function check(str)
{
let n = str.length;
// Compute sum of even and odd digit
// sums
let oddDigSum = 0, evenDigSum = 0;
for (let i = 0; i < n; i++)
{
// When i is even, position of
// digit is odd
if (i % 2 == 0)
oddDigSum += (str[i] - '0' );
else
evenDigSum += (str[i] - '0' );
}
// Check its difference is
// divisible by 11 or not
return ((oddDigSum - evenDigSum)
% 11 == 0);
}
// Driver Code let str = "76945" ;
if (check(str))
document.write( "Yes" );
else
document.write( "No" );
// This code is contributed by chinmoy1997pal. </script> |
<?php // PHP program to find if a // number is divisible by // 11 or not // Function to find that number // divisible by 11 or not function check( $str )
{ $n = strlen ( $str );
// Compute sum of even
// and odd digit sums
$oddDigSum = 0; $evenDigSum = 0;
for ( $i = 0; $i < $n ; $i ++)
{
// When i is even, position
// of digit is odd
if ( $i % 2 == 0)
$oddDigSum += ( $str [ $i ] - '0' );
else
$evenDigSum += ( $str [ $i ] - '0' );
}
// Check its difference
// is divisible by 11 or not
return (( $oddDigSum - $evenDigSum )
% 11 == 0);
} // Driver code $str = "76945" ;
$x = check( $str )? "Yes" : "No " ;
echo ( $x );
// This code is contributed by Ajit. ?> |
Yes
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++ code to check whether // the given number is divisible by 11 or not #include <bits/stdc++.h> using namespace std;
int main()
{ // input
long long n = 1234567589333892;
// the above input can also be given as n=input() ->
// taking input from user finding given number is
// divisible by 11 or not
if (n % 11 == 0)
cout << "Yes" << endl;
else
cout << "No" << endl;
} // This code is contributed by phasing17 |
// Java code to check whether // the given number is divisible by 11 or not import java.io.*;
class GFG {
public static void main(String[] args)
{
// input
Long n = Long.parseUnsignedLong( "1234567589333892" );
//finding given number is
// divisible by 11 or not
if (n % 11 == 0 )
System.out.println( "Yes" );
else
System.out.println( "No" );
}
} // This code is contributed by phasing17 |
# Python code # To check whether the given number is divisible by 11 or not #input n = 1234567589333892
# the above input can also be given as n=input() -> taking input from user # finding given number is divisible by 11 or not if int (n) % 11 = = 0 :
print ( "Yes" )
else :
print ( "No" )
# this code is contributed by gangarajula laxmi
|
// C# code to check whether // the given number is divisible by 11 or not using System;
class GFG {
public static void Main( string [] args)
{
// input
long n = 1234567589333892;
// the above input can also be given as n=input() ->
// taking input from user finding given number is
// divisible by 11 or not
if (n % 11 == 0)
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
} // This code is contributed by phasing17 |
// JavaScript code to check whether // the given number is divisible by 11 or not // input let n = 1234567589333892 // the above input can also be given as n=input() -> taking input from user // finding given number is divisible by 11 or not if (n % 11 == 0)
console.log( "Yes" )
else console.log( "No" )
// this code is contributed by phasing17 |
<?php $num = 1234567589333892;
// checking if the given number is divisible by 37 or
// not using modulo division operator if the output of
// num%37 is equal to 0 then given number is divisible
// by 37 otherwise not divisible by 37
if ( $num % 11 == 0) {
echo "Yes" ;
}
else {
echo "No" ;
}
?> |
Yes
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++ program to check if given number is divisible by 11 // or not using modulo division #include <iostream> using namespace std;
int main()
{ // input number
int num = 76945;
// checking if the given number is divisible by 11 or
// not using modulo division operator if the output of
// num%11 is equal to 0 then given number is divisible
// by 11 otherwise not divisible by 11
if (num % 11 == 0) {
cout << " divisible" ;
}
else {
cout << " not divisible" ;
}
return 0;
} // this code is contributed by gangarajula laxmi |
// java program to check if given number is divisible by 11 // or not using modulo division import java.io.*;
class GFG {
public static void main(String[] args)
{
// input number
int num = 76945 ;
// checking if the given number is divisible by 11
// or not
// using modulo division operator if the output of
// num%11 is equal to 0 then given number is
// divisible by 11 otherwise not divisible by 11
if (num % 11 == 0 ) {
System.out.println( " divisible" );
}
else {
System.out.println( " not divisible" );
}
}
} // this code is contributed by gangarajula laxmi |
# Python3 code for the above approach # To check whether the given number is divisible by 11 or not # input n = 76945
# finding given number is divisible by 11 or not if (n % 11 = = 0 ):
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by phasing17 |
using System;
public class GFG {
static public void Main()
{
// input number
double num = 76945;
// checking if the given number is divisible by 11
// or not using modulo division operator if the
// output of num%11 is equal to 0 then given number
// is divisible by 11 otherwise not divisible by 11
if (num % 11 == 0) {
Console.Write( " divisible" );
}
else {
Console.Write( " not divisible" );
}
}
// this code is contributed by gangarajula laxmi
|
<script> // JavaScript code for the above approach
// To check whether the given number is divisible by 11 or not
// input
var n = 76945
// finding given number is divisible by 11 or not
if (n % 11 == 0)
document.write( "Yes" )
else
document.write( "No" )
// This code is contributed by laxmigangarajula03 </script>
|
<?php // PHP program to check // if a large number is // divisible by 11. // Driver Code
// input number
$num = 76945;
// finding given number is divisible by 11 or not if ( $num % 11 == 0)
echo " divisible" ;
else echo "not divisible" ;
// This code is contributed by satwik4409. ?> |
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.
#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;
}
// checking if divisible by 11 or not
return alternating_sum % 11 == 0;
} int main() {
std::cout << std::boolalpha; // enable printing of true/false instead of 1/0
std::cout << is_divisible_by_11(11) << std::endl; // true
std::cout << is_divisible_by_11(22) << std::endl; // true
std::cout << is_divisible_by_11(121) << std::endl; // true
std::cout << is_divisible_by_11(10) << std::endl; // false
return 0;
} |
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 ;
}
// Checking if divisible by 11 or not
return alternatingSum % 11 == 0 ;
}
public static void main(String[] args)
{
System.out.println(isDivisibleBy11( 11 )); // true
System.out.println(isDivisibleBy11( 22 )); // true
System.out.println(isDivisibleBy11( 121 )); // true
System.out.println(isDivisibleBy11( 10 )); // false
}
} |
def is_divisible_by_11(num):
alternating_sum = 0
multiplier = 1
while num > 0 :
alternating_sum + = multiplier * (num % 10 )
multiplier * = - 1
num / / = 10
#checking if divisible by 11 or not
return alternating_sum % 11 = = 0
print (is_divisible_by_11( 11 )) # True
print (is_divisible_by_11( 22 )) # True
print (is_divisible_by_11( 121 )) # True
print (is_divisible_by_11( 10 )) # False
|
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;
}
// checking if divisible by 11 or not
return alternatingSum % 11 == 0;
}
static void Main()
{
Console.WriteLine(IsDivisibleBy11(11)); // true
Console.WriteLine(IsDivisibleBy11(22)); // true
Console.WriteLine(IsDivisibleBy11(121)); // true
Console.WriteLine(IsDivisibleBy11(10)); // false
}
} |
function is_divisible_by_11(num)
{ // Initialize an integer named "alternating_sum" to 0
let alternating_sum = 0;
// Initialize an integer named "multiplier" to 1
let multiplier = 1;
// Loop through the digits of the input integer from right to left
while (num > 0)
{
// Add the product of the current digit and "multiplier" to "alternating_sum"
alternating_sum += multiplier * (num % 10);
// Toggle the value of "multiplier" between 1 and -1
multiplier *= -1;
// Remove the last digit from the input integer
num = Math.floor(num / 10);
}
// Check if "alternating_sum" is divisible by 11
return alternating_sum % 11 == 0;
} // Enable printing of true/false instead of 1/0 console.log(is_divisible_by_11(11)); // true
console.log(is_divisible_by_11(22)); // true
console.log(is_divisible_by_11(121)); // true
console.log(is_divisible_by_11(10)); // false
|
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 (%).
#include <iostream> #include <string> bool isDivisibleBy11( const std::string &number) {
int alternatingSum = 0;
bool subtract = true ; // Start by subtracting the digits
for ( char digit : number) {
int digitValue = digit - '0' ;
if (subtract) {
alternatingSum -= digitValue;
} else {
alternatingSum += digitValue;
}
subtract = !subtract; // Toggle subtraction for the next digit
}
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;
} |
public class Main {
// Function to check if a given string is divisible by 11
public static boolean isDivisibleBy11(String number) {
int alternatingSum = 0 ;
boolean subtract = true ; // Start by subtracting the digits
// Iterate through each digit in the string
for ( char digit : number.toCharArray()) {
int digitValue = digit - '0' ; // Convert character to integer
// Depending on the "subtract" flag, either subtract or add the digit value to the sum
if (subtract) {
alternatingSum -= digitValue;
} else {
alternatingSum += digitValue;
}
subtract = !subtract; // Toggle subtraction for the next digit
}
// Check if the alternating sum is divisible by 11
return (alternatingSum % 11 == 0 );
}
public static void main(String[] args) {
String st = "76945" ;
if (isDivisibleBy11(st)) {
System.out.println( "Yes" ); // If divisible by 11, print "Yes"
} else {
System.out.println( "No" ); // If not divisible by 11, print "No"
}
}
} |
def is_divisible_by_11(number):
# Compute the alternating sum of the digits from right to left
alternating_sum = 0
for i, digit in enumerate ( reversed ( str (number))):
alternating_sum + = ( - 1 ) * * i * int (digit)
# If the alternating sum is divisible by 11, then the number is divisible by 11
return alternating_sum % 11 = = 0
st = "76945"
if (is_divisible_by_11(st)) :
print ( "Yes" )
else :
print ( "No " )
|
using System;
class Program
{ // Function to check if a string is divisible by 11 using the alternating sum method
static bool IsDivisibleBy11( string number)
{
int alternatingSum = 0;
bool subtract = true ; // Start by subtracting the digits
// Loop through each digit in the string
foreach ( char digit in number)
{
int digitValue = digit - '0' ; // Convert the character digit to its integer value
if (subtract)
{
alternatingSum -= digitValue; // Subtract the digit value from the alternating sum
}
else
{
alternatingSum += digitValue; // Add the digit value to the alternating sum
}
subtract = !subtract; // Toggle subtraction for the next digit
}
// Check if the alternating sum is divisible by 11
return (alternatingSum % 11 == 0);
}
static void Main()
{
string st = "76945" ;
if (IsDivisibleBy11(st))
{
Console.WriteLine( "Yes" ); // The string is divisible by 11
}
else
{
Console.WriteLine( "No" ); // The string is not divisible by 11
}
}
} |
// Function to check if a given number is divisible by 11 function isDivisibleBy11(number) {
// Initialize alternating sum and subtraction flag
let alternatingSum = 0;
let subtract = true ;
// Loop through each digit of the number
for (let i = 0; i < number.length; i++) {
// Get the numeric value of the current digit
let digitValue = parseInt(number[i], 10);
// Subtract or add the digit to the alternating sum based on the flag
if (subtract) {
alternatingSum -= digitValue;
} else {
alternatingSum += digitValue;
}
// Toggle the subtraction flag for the next iteration
subtract = !subtract;
}
// Check if the alternating sum is divisible by 11
return alternatingSum % 11 === 0;
} // Test the function with a sample string 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" );
} |
Yes
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 .