Given a large number as a string s, determine if it is divisible by 9.
Note: The number might be so large that it can't be stored in standard data types like long long.
Examples:
Input : s = "69354"
Output: Yes
Explanation: 69354 is divisible by 9.
Input: s = "234567876799333"
Output: No
Explanation: 234567876799333 is not divisible by 9.
Input: s = "3635883959606670431112222"
Output: No
Explanation: 3635883959606670431112222 is not divisible by 9.
We can solve this by using this property that a number is divisible by 9 if sum of its digits is divisible by 9.
Let us take an example, s = "69354"
Sum of digits = 6 + 9 + 3 + 5 + 4 = 27
Since 27 is divisible by 9.So Answer will be Yes.
How does this work?
Consider the number 1332. We can break it down as follows:
69354 = 6×10000 + 9×1000 + 3×100 + 5×10 + 4
Key Observation:
When dividing powers of 10 by 9, the remainder is always 1. This means that:
Remainder of 10i (for any i) when divided by 9 is 1.
This property allows us to simplify the number for divisibility by 9.
Proof:
Now, let’s find the remainder when dividing 69354 = 6×10000 + 9×1000 + 3×100 + 5×10 + 4 by 9.
- The remainder when dividing 6×10000, 9×1000, 3×100, 5×10 and 4 by 9 is the same as the remainder when dividing:
6×1 + 9×1 + 3×1 + 5×1 +4
- This simplifies to:
6 + 9 + 3 + 5 + 4 = 27
Since 27 is divisible by 9, the number 69354 is also divisible by 9.
#include<iostream>
using namespace std;
// Function to find that number divisible by 9 or not
int check(string s)
{
// Compute sum of digits
int n = s.length();
int sum = 0;
for (int i=0; i<n; i++)
sum += (s[i]-'0');
// Check if sum of digits is divisible by 9.
return (sum % 9 == 0);
}
int main()
{
string s = "69354";
check(s)? cout << "Yes" : cout << "No ";
return 0;
}
import java.util.*;
// Function to find that number divisible by 9 or not
public class GfG{
static boolean check(String s) {
// Compute sum of digits
int sum = 0;
for (int i = 0; i < s.length(); i++)
sum += (s.charAt(i) - '0');
// Check if sum of digits is divisible by 9.
return (sum % 9 == 0);
}
public static void main(String[] args) {
String s = "69354";
System.out.println(check(s) ? "Yes" : "No");
}
}
# Function to find that number divisible by 9 or not
def check(s):
# Compute sum of digits
sum_digits = sum(int(digit) for digit in s)
# Check if sum of digits is divisible by 9.
return sum_digits % 9 == 0
s = "69354"
print("Yes" if check(s) else "No")
using System;
class GfG{
static bool Check(string s) {
// Compute sum of digits
int sum = 0;
foreach (char c in s)
sum += (c - '0');
// Check if sum of digits is divisible by 9.
return sum % 9 == 0;
}
static void Main() {
string s = "69354";
Console.WriteLine(Check(s) ? "Yes" : "No");
}
}
function check(s) {
// Compute sum of digits
let sum = 0;
for (let i = 0; i < s.length; i++)
sum += (s.charAt(i) - '0');
// Check if sum of digits is divisible by 9.
return sum % 9 === 0;
}
let s = "69354";
console.log(check(s) ? "Yes" : "No");
Output
Yes
Time Complexity: O(n), n is length of string.
Auxiliary Space: O(1)