Check if the number is divisible 43 or not - GeeksforGeeks

24 Nov, 2021
Given a number N, the task is to check whether the number is divisible by 43 or not. Examples:  
Input: N = 2795 Output: yes Explanation: 43 * 65 = 2795Input: N = 11094 Output: yes Explanation: 43 * 258 = 11094  
 
Approach: The divisibility test of 43 is:  
Extract the last digit.Add 13 * last digit from the remaining number obtained after removing the last digit.Repeat the above steps until a two-digit number, or zero, is obtained.If the two-digit number is divisible by 43, or it is 0, then the original number is also divisible by 43.
Extract the last digit.
Add 13 * last digit from the remaining number obtained after removing the last digit.
Repeat the above steps until a two-digit number, or zero, is obtained.
If the two-digit number is divisible by 43, or it is 0, then the original number is also divisible by 43.
For example:  
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
Below is the implementation of the above approach:  
C++
Java
Python 3
C#
Javascript
// C++ program to check whether a number// is divisible by 43 or not #include<bits/stdc++.h>#include<stdlib.h> using namespace std;// Function to check if the number is  divisible by 43 or notbool 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 Codeint main() {    int N = 2795;      if (isDivisible(N))        cout<<"Yes"<<endl ;    else        cout<<"No"<<endl ;         return 0;    }    // This code is contributed by ANKITKUMAR34
// Java program to check whether a number// is divisible by 43 or notclass GFG{ // Function to check if the number is  divisible by 43 or notstatic 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 Codepublic 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 program to check whether a number# is divisible by 43 or not # Function to check if the number is# divisible by 43 or notdef 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 Codeif __name__ == "__main__" :         N = 2795     if (isDivisible(N)):        print("Yes")    else :        print("No")
// C# program to check whether a number// is divisible by 43 or notusing System;         class GFG{     // Function to check if the number is divisible by 43 or notstatic 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 Codepublic static void Main(){    int N = 2795;     if (isDivisible(N))        Console.WriteLine("Yes");    else        Console.WriteLine("No");    }} // This code is contributed by AbhiThakur
<script>//javascript program to check whether a number// is divisible by 43 or not // Function to check if the number is divisible by 43 or notfunction isDivisible(n){    let d;    // While there are at least two digits    while(parseInt(n/100) > 0)    {         // Extracting the last        d = n % 10;         // Truncating the number    n = parseInt(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    let N = 2795;     if (isDivisible(N))        document.write("Yes");    else        document.write("No"); // This code is contributed by vaibhavrabadiya117.</script>
Yes
 
Time Complexity: O(log10N)
Auxiliary Space: O(1)
ANKITKUMAR34
princiraj1992
abhaysingh290895
vaibhavrabadiya117
rishavmahato348
divisibility
Number Divisibility
Aptitude
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Puzzle | How much money did the man have before entering the bank?
7 Best Tips to Prepare for Aptitude Test For Campus Placements
Puzzle | Splitting a Cake with a Missing Piece in two equal portion
Geometry and Co-ordinates
Program to find the last two digits of x^y
Program for Fibonacci numbers
C++ Data Types
Write a program to print all permutations of a given string
Set in C++ Standard Template Library (STL)
Program to find GCD or HCF of two numbers