Check if the given number is divisible by 71 or not

Given a number N, the task is to check whether the number is divisible by 71 or not.

Examples:

Input: N = 25411681
Output: yes
Explanation:
71 * 357911 = 25411681

Input: N = 5041
Output: yes
Explanation:
71 * 71 = 5041

Approach: The divisibility test of 71 is:



  1. Extract the last digit.
  2. Subtract 7 * last digit from the remaining number obtained after removing the last digit.
  3. Repeat the above steps until a two-digit number, or zero, is obtained.
  4. If the two-digit number is divisible by 71, or it is 0, then the original number is also divisible by 71.

For example:

If N = 5041

Step 1:
  N = 5041
  Last digit = 1
  Remaining number = 504
  Subtracting 7 times last digit
  Resultant number = 504 - 7*1 = 497

Step 2:
  N = 497
  Last digit = 7
  Remaining number = 49
  Subtracting 7 times last digit
  Resultant number = 49 - 7*7 = 0

Step 3:
  N = 0
  Since N is a two-digit number,
  and 0 is divisible by 71

Therefore N = 5041 is also divisible by 71

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check whether a number
// is divisible by 71 or not
#include<bits/stdc++.h>
#include<stdlib.h>
  
using namespace std;
  
// Function to check if the number is divisible by 71 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;
  
        // Subtracting seven times the last 
        // digit to the remaining number 
        n = abs(n - (d * 7));
    }
    // Finally return if the two-digit
    // number is divisible by 71 or not
    return (n % 71 == 0) ;
}
  
// Driver Code 
int main() {
    int N = 5041;
  
    if (isDivisible(N)) 
        cout << "Yes" << endl ;
    else
        cout << "No" << endl ;
      
    return 0;     
  
// This code is contributed by ANKITKUMAR34

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check whether a number
// is divisible by 71 or not
import java.util.*;
  
class GFG{
  
// Function to check if the number is divisible by 71 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;
      
            // Subtracting seven times the last 
            // digit to the remaining number 
            n = Math.abs(n - (d * 7));
        }
  
        // Finally return if the two-digit
        // number is divisible by 71 or not
        return (n % 71 == 0) ;
    }
      
    // Driver Code 
    public static void main(String args[]){
        int N = 5041;
      
        if (isDivisible(N)) 
            System.out.println("Yes") ;
        else
            System.out.println("No");
    
}
  
// This code is contributed by AbhiThakur

chevron_right


Python 3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to check whether a number
# is divisible by 71 or not
  
# Function to check if the number is 
# divisible by 71 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
  
        # Subtracting seven times the last 
        # digit to the remaining number 
        n = abs(n-(d * 7))
  
    # Finally return if the two-digit
    # number is divisible by 71 or not
    return (n % 71 == 0
  
# Driver Code 
if __name__ == "__main__"
      
    N = 5041
  
    if (isDivisible(N)) : 
        print("Yes"
    else
        print("No"

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check whether a number
// is divisible by 71 or not
using System; 
          
class GFG 
      
// Function to check if the number is divisible by 71 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;
      
        // Subtracting fourteen times the last 
        // digit to the remaining number 
        n = Math.Abs(n - (d * 7));
    }
      
    // Finally return if the two-digit
    // number is divisible by 71 or not
    return (n % 71 == 0);
}
      
// Driver Code 
public static void Main() 
    int N = 5041;
      
    if (isDivisible(N)) 
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
  
// This code is contributed by mohit kumar 29. 

chevron_right


Output:

Yes

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.