📜  检查数字是否可整除43

📅  最后修改于: 2021-05-13 22:04:14             🧑  作者: Mango

给定数字N ,任务是检查数字是否可被43整除。

例子:

方法: 43的除法检验是:

  1. 提取最后一位数字。
  2. 从除去最后一位后获得的剩余号码中添加13 *最后一位。
  3. 重复上述步骤,直到获得两位数字或零。
  4. 如果两位数可被43整除或为0,则原始数字也可被43整除。

例如:

If N = 11739

Step 1:
  N = 11739
  Last digit = 9
  Remaining number = 1173
  Adding 13 times last digit
  Resultant number = 1173 + 13*9 = 1290

Step 2:
  N = 1290
  Since 129 is divisible by 43 as 43 * 3 = 129

Therefore N = 11739 is also divisible by 43

下面是上述方法的实现:

C++
// C++ program to check whether a number
// is divisible by 43 or not
  
#include
#include
  
using namespace std;
// Function to check if the number is  divisible by 43 or not 
bool isDivisible(int n)  
{
    int d;
    // While there are at least two digits 
    while (n / 100) 
    {
   
        // Extracting the last 
        d = n % 10;
   
        // Truncating the number 
        n /= 10;
   
        // adding thirteen times the last 
        // digit to the remaining number 
        n = abs(n+(d * 13));
    }
    // Finally return if the two-digit
    // number is divisible by 43 or not
    return (n % 43 == 0) ;
}
  
// Driver Code 
int main() {
    int N = 2795;
   
    if (isDivisible(N)) 
        cout<<"Yes"<


Java
// Java program to check whether a number
// is divisible by 43 or not
class GFG
{
  
// Function to check if the number is  divisible by 43 or not 
static boolean isDivisible(int n)  
{
    int d;
    // While there are at least two digits 
    while ((n / 100) > 0) 
    {
    
        // Extracting the last 
        d = n % 10;
    
        // Truncating the number 
        n /= 10;
    
        // adding thirteen times the last 
        // digit to the remaining number 
        n = Math.abs(n+(d * 13));
    }
    // Finally return if the two-digit
    // number is divisible by 43 or not
    return (n % 43 == 0) ;
}
   
// Driver Code 
public static void main(String[] args) {
    int N = 2795;
    
    if (isDivisible(N)) 
        System.out.print("Yes");
    else
        System.out.print("No");
      
 }     
}    
   
// This code is contributed by PrinciRaj1992


Python 3
# Python program to check whether a number
# is divisible by 43 or not
  
# Function to check if the number is 
# divisible by 43 or not 
def isDivisible(n) : 
  
    # While there are at least two digits 
    while n // 100 : 
  
        # Extracting the last 
        d = n % 10
  
        # Truncating the number 
        n //= 10
  
        # Adding thirteen  times the last 
        # digit to the remaining number 
        n = abs(n+(d * 13))
  
    # Finally return if the two-digit
    # number is divisible by 43 or not
    return (n % 43 == 0) 
  
# Driver Code 
if __name__ == "__main__" : 
      
    N = 2795
  
    if (isDivisible(N)): 
        print("Yes") 
    else : 
        print("No")


C#
// C# program to check whether a number
// is divisible by 43 or not
using System; 
          
class GFG 
{ 
      
// Function to check if the number is divisible by 43 or not 
static bool isDivisible(int n) 
{
    int d;
      
    // While there are at least two digits 
    while (n / 100 > 0) 
    {
  
        // Extracting the last 
        d = n % 10;
  
        // Truncating the number 
        n /= 10;
  
        // adding thirteen times the last 
        // digit to the remaining number 
        n = Math.Abs(n + (d * 13));
    }
      
    // Finally return if the two-digit
    // number is divisible by 43 or not
    return (n % 43 == 0) ;
}
  
// Driver Code 
public static void Main() 
{ 
    int N = 2795;
  
    if (isDivisible(N)) 
        Console.WriteLine("Yes"); 
    else
        Console.WriteLine("No");     
} 
}
  
// This code is contributed by AbhiThakur


输出:
Yes