给定一个数字,任务是检查一个数字是否可以被4整除。输入的数字可能很大,即使使用long int,也可能无法存储。
null
例如:
Input : n = 1124Output : YesInput : n = 1234567589333862Output : NoInput : n = 363588395960667043875487Output : No
由于输入的数字可能非常大,我们不能使用n%4来检查一个数字是否可以被4整除,尤其是在C/C++等语言中。这个想法基于以下事实。
如果一个数的最后两位数字可以被4整除,则该数可以被4整除。
插图:
For example, let us consider 76952 Number formed by last two digits = 52Since 52 is divisible by 4, answer is YES.
这是怎么回事?
Let us consider 76952, we can write it as76942 = 7*10000 + 6*1000 + 9*100 + 5*10 + 2The proof is based on below observation:Remainder of 10i divided by 4 is 0 if i greater than or equal to two. Note than 100, 1000,... etc lead to remainder 0 when divided by 4.So remainder of "7*10000 + 6*1000 + 9*100 + 5*10 + 2" divided by 4 is equivalent to remainder of following : 0 + 0 + 0 + 5*10 + 2 = 52Therefore we can say that the whole number is divisible by 4 if 52 is divisible by 4.
以下是上述想法的实施:
C++
// C++ program to find if a number is divisible by // 4 or not #include <bits/stdc++.h> using namespace std; // Function to find that number divisible by // 4 or not bool check(string str) { int n = str.length(); // Empty string if (n == 0) return false ; // If there is single digit if (n == 1) return ((str[0] - '0' ) % 4 == 0); // If number formed by last two digits is // divisible by 4. int last = str[n - 1] - '0' ; int second_last = str[n - 2] - '0' ; return ((second_last * 10 + last) % 4 == 0); } // Driver code int main() { string str = "76952" ; // Function call check(str) ? cout << "Yes" : cout << "No " ; return 0; } |
JAVA
// Java program to find if a number is // divisible by 4 or not class IsDivisible { // Function to find that number // is divisible by 4 or not static boolean check(String str) { int n = str.length(); // Empty string if (n == 0 ) return false ; // If there is single digit if (n == 1 ) return ((str.charAt( 0 ) - '0' ) % 4 == 0 ); // If number formed by last two digits is // divisible by 4. int last = str.charAt(n - 1 ) - '0' ; int second_last = str.charAt(n - 2 ) - '0' ; return ((second_last * 10 + last) % 4 == 0 ); } // Driver code public static void main(String[] args) { String str = "76952" ; // Function call if (check(str)) System.out.println( "Yes" ); else System.out.println( "No" ); } } |
Python3
# Python 3 program to find # if a number is divisible # by 4 or not # Function to find that # number divisible by # 4 or not def check(st): n = len (st) # Empty string if (n = = 0 ): return False # If there is single # digit if (n = = 1 ): return ((st[ 0 ] - '0' ) % 4 = = 0 ) # If number formed by # last two digits is # divisible by 4. last = ( int )(st[n - 1 ]) second_last = ( int )(st[n - 2 ]) return ((second_last * 10 + last) % 4 = = 0 ) # Driver code st = "76952" # Function call 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 4 or not using System; class GFG { // Function to find that number // is divisible by 4 or not static bool check(String str) { int n = str.Length; // Empty string if (n == 0) return false ; // If there is single digit if (n == 1) return ((str[0] - '0' ) % 4 == 0); // If number formed by last two // digits is divisible by 4. int last = str[n - 1] - '0' ; int second_last = str[n - 2] - '0' ; return ((second_last * 10 + last) % 4 == 0); } // Driver code public static void Main() { String str = "76952" ; // Function call 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 // 4 or not // Function to find that // number divisible by // 4 or not function check( $str ) { $n = strlen ( $str ); // Empty string if ( $n == 0) return false; // If there is single digit if ( $n == 1) return (( $str [0] - '0' ) % 4 == 0); // If number formed by // last two digits is // divisible by 4. $last = $str [ $n - 1] - '0' ; $second_last = $str [ $n - 2] - '0' ; return (( $second_last * 10 + $last ) % 4 == 0); } // Driver code $str = "76952" ; // Function call $x = check( $str )? "Yes" : "No" ; echo ( $x ); // This code is contributed by Ajit. ?> |
Javascript
//Javascript program to check whether a string is divisible by 4 or not // function to check the divisibility function check(str) { // checking the length for future reference var n = str.length; // if it is empty then directly returning false if ( n == 0) { return false ; } if ( n == 1) { return ((str[0] - '0' ) % 4 == 0); } var lastNumber = str[n-1] - '0' ; var lastSecondNUmber = str[n-2] - '0' ; return ((lastSecondNUmber * 10 + lastNumber) % 4 == 0); } // Driver code var str= "76952" ; //checking the value by passing it into the function // Function call if (check(str)){ console.log( "Yes" ); } else { console.log( "No" ); } |
输出
Yes
本文由 丹麦拉扎 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END