Check if a large number is divisible by 9 or not
Last Updated :
13 Sep, 2023
Given a number, the task is to find if the number is divisible by 9 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 = 69354
Output : Yes
Input : n = 234567876799333
Output : No
Input : n = 3635883959606670431112222
Output : No
Since input number may be very large, we cannot use n % 9 to check if a number is divisible by 9 or not, especially in languages like C/C++. The idea is based on following fact.
A number is divisible by 9 if sum of its digits is divisible by 9.
Illustration:
For example n = 9432
Sum of digits = 9 + 4 + 3 + 2
= 18
Since sum is divisible by 9,
answer is Yes.
How does this work?
Let us consider 1332, we can write it as
1332 = 1*1000 + 3*100 + 3*10 + 2
The proof is based on below observation:
Remainder of 10i divided by 9 is 1
So powers of 10 only results in remainder 1
when divided by 9.
Remainder of "1*1000 + 3*100 + 3*10 + 2"
divided by 9 can be written as :
1*1 + 3*1 + 3*1 + 2 = 9
The above expression is basically sum of
all digits.
Since 9 is divisible by 9, answer is yes.
Below is the implementation of the above idea.
C++
#include<bits/stdc++.h>
using namespace std;
int check(string str)
{
int n = str.length();
int digitSum = 0;
for ( int i=0; i<n; i++)
digitSum += (str[i]- '0' );
return (digitSum % 9 == 0);
}
int main()
{
string str = "99333" ;
check(str)? cout << "Yes" : cout << "No " ;
return 0;
}
|
Java
import java.io.*;
class IsDivisible
{
static boolean check(String str)
{
int n = str.length();
int digitSum = 0 ;
for ( int i= 0 ; i<n; i++)
digitSum += (str.charAt(i)- '0' );
return (digitSum % 9 == 0 );
}
public static void main (String[] args)
{
String str = "99333" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def check(st) :
n = len (st)
digitSum = 0
for i in range ( 0 ,n) :
digitSum = digitSum + ( int )(st[i])
return (digitSum % 9 = = 0 )
st = "99333"
if (check(st)) :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG {
static bool check(String str)
{
int n = str.Length;
int digitSum = 0;
for ( int i = 0; i < n; i++)
digitSum += (str[i] - '0' );
return (digitSum % 9 == 0);
}
public static void Main ()
{
String str = "99333" ;
if (check(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function check( $str )
{
$n = strlen ( $str );
$digitSum = 0;
for ( $i = 0; $i < $n ; $i ++)
$digitSum += ( $str [ $i ] - '0' );
return ( $digitSum % 9 == 0);
}
$str = "99333" ;
$x = check( $str ) ? "Yes" : "No " ;
echo ( $x );
?>
|
Javascript
<script>
function check(str)
{
let n = str.length;
let digitSum = 0;
for (let i = 0; i < n; i++)
digitSum += (str[i] - '0' );
return (digitSum % 9 == 0);
}
let str = "99333" ;
let x = check(str) ? "Yes" : "No " ;
document.write(x);
</script>
|
Time Complexity: O(logN), as we are traversing the digits which will effectively costs logN time.
Auxiliary Space: O(1), as we are not using any extra space.
Method 2: Checking given number is divisible by 9 or not by using the modulo division operator “%”.
C++
#include <iostream>
using namespace std;
int main() {
long long int n = 3635883959606670431112222;
if (n % 9 == 0) {
cout<< "Yes" ;
}
else {
cout<< "No" ;
}
return 0;
}
|
Java
import java.io.*;
import java.math.BigInteger;
class GFG {
public static void main (String[] args)
{
BigInteger n, b1,b2;
n = new BigInteger( "3635883959606670431112222" );
b1 = new BigInteger( "9" );
BigInteger result = n.mod(b1);
b2 = new BigInteger( "0" );
int comparevalue = result.compareTo(b2);
if (comparevalue== 0 ) {
System.out.println( "Yes" );
}
else {
System.out.println( "No" );
}
}
}
|
Python3
n = 3635883959606670431112222
if int (n) % 9 = = 0 :
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
public class GFG {
static public void Main()
{
double n = 36358839596066;
if (n % 9 == 0) {
Console.Write( "Yes" );
}
else {
Console.Write( "No" );
}
}
}
|
Javascript
<script>
var n=3635883959606670431112222
if (n%9==0)
document.write( "Yes" )
else
document.write( "No" )
</script>
|
PHP
<?php
$num = 3635883959606670431112222;
if ( $num % 9 == 0)
echo " divisible" ;
else
echo "not divisible" ;
?>
|
Time complexity: O(1) it is performing constant operations
Auxiliary space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...