检查一个大数字是否可以被9整除

给定一个数字,任务是找出这个数字是否可以被9整除。输入的数字可能很大,即使使用long int,也可能无法存储。

null

例如:

Input  : n = 69354Output : YesInput  : n = 234567876799333Output : NoInput  : n = 3635883959606670431112222Output : No

由于输入的数字可能非常大,我们不能使用n%9来检查一个数字是否可以被9整除,尤其是在C/C++等语言中。这个想法基于以下事实。

A number is divisible by 9 if sum of its digits is divisible by 9.

插图:

For example n = 9432Sum of digits = 9 + 4 + 3 + 2             = 18Since sum is divisible by 9,answer is Yes.

这是怎么回事?

Let us consider 1332, we can write it as1332 = 1*1000 + 3*100 + 3*10 + 2The proof is based on below observation:Remainder of 10i divided by 9 is 1So 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 = 9The above expression is basically sum ofall digits.Since 9 is divisible by 9, answer is yes.

下面是上述想法的实现。

C++

// C++ program to find if a number is divisible by
// 9 or not
#include<bits/stdc++.h>
using namespace std;
// Function to find that number divisible by 9 or not
int check(string str)
{
// Compute sum of digits
int n = str.length();
int digitSum = 0;
for ( int i=0; i<n; i++)
digitSum += (str[i]- '0' );
// Check if sum of digits is divisible by 9.
return (digitSum % 9 == 0);
}
// Driver code
int main()
{
string str = "99333" ;
check(str)?  cout << "Yes" : cout << "No " ;
return 0;
}


JAVA

// Java program to find if a number is
// divisible by 9 or not
class IsDivisible
{
// Function to find that number
// is divisible by 9 or not
static boolean check(String str)
{
// Compute sum of digits
int n = str.length();
int digitSum = 0 ;
for ( int i= 0 ; i<n; i++)
digitSum += (str.charAt(i)- '0' );
// Check if sum of digits is divisible by 9.
return (digitSum % 9 == 0 );
}
// main function
public static void main (String[] args)
{
String str = "99333" ;
if (check(str))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}


Python3

# Python 3 program to
# find if a number is
# divisible by
# 9 or not
# Function to find that
# number divisible by 9
# or not
def check(st) :
# Compute sum of digits
n = len (st)
digitSum = 0
for i in range ( 0 ,n) :
digitSum = digitSum + ( int )(st[i])
# Check if sum of digits
# is divisible by 9.
return (digitSum % 9 = = 0 )
# Driver code
st = "99333"
if (check(st)) :
print ( "Yes" )
else :
print ( "No" )
# This code is contributed by Nikita Tiwari.


C#

// C# program to find if a number is
// divisible by 9 or not.
using System;
class GFG {
// Function to find that number
// is divisible by 9 or not
static bool check(String str)
{
// Compute sum of digits
int n = str.Length;
int digitSum = 0;
for ( int i = 0; i < n; i++)
digitSum += (str[i] - '0' );
// Check if sum of digits is
// divisible by 9.
return (digitSum % 9 == 0);
}
// main function
public static void Main ()
{
String str = "99333" ;
if (check(str))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
// This code is Contributed by
// nitin mittal.


PHP

<?php
// PHP program to find if a number
// is divisible by 9 or not
// Function to find that
// number divisible by 9 or not
function check( $str )
{
// Compute sum of digits
$n = strlen ( $str );
$digitSum = 0;
for ( $i = 0; $i < $n ; $i ++)
$digitSum += ( $str [ $i ] - '0' );
// Check if sum of digits
// is divisible by 9.
return ( $digitSum % 9 == 0);
}
// Driver code
$str = "99333" ;
$x = check( $str ) ? "Yes" : "No " ;
echo ( $x );
// This code is contributed by Ajit.
?>


Javascript

<script>
// Javascript program to find if a number
// is divisible by 9 or not
// Function to find that
// number divisible by 9 or not
function check(str)
{
// Compute sum of digits
let n = str.length;
let digitSum = 0;
for (let i = 0; i < n; i++)
digitSum += (str[i] - '0' );
// Check if sum of digits
// is divisible by 9.
return (digitSum % 9 == 0);
}
// Driver code
let str = "99333" ;
let x = check(str) ? "Yes" : "No " ;
document.write(x);
// This code is contributed by _saurabh_jaiswal.
</script>


输出:

Yes

本文由 丹麦拉扎 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享