Check divisibility by 7

07 Jul, 2022
Given a number, check if it is divisible by 7. You are not allowed to use modulo operator, floating point arithmetic is also not allowed. A simple method is repeated subtraction. Following is another interesting method.Divisibility by 7 can be checked by a recursive method. A number of the form 10a + b is divisible by 7 if and only if a – 2b is divisible by 7. In other words, subtract twice the last digit from the number formed by the remaining digits. Continue to do this until a small number. Example: the number 371: 37 – (2×1) = 37 – 2 = 35; 3 – (2 × 5) = 3 – 10 = -7; thus, since -7 is divisible by 7, 371 is divisible by 7. Following is the implementation of the above method  
C++
C
Java
Python3
C#
PHP
Javascript
// A Program to check whether a number is divisible by 7#include <bits/stdc++.h>using namespace std; int isDivisibleBy7( int num ){    // If number is negative, make it positive    if( num < 0 )        return isDivisibleBy7( -num );     // Base cases    if( num == 0 || num == 7 )        return 1;    if( num < 10 )        return 0;     // Recur for ( num / 10 - 2 * num % 10 )    return isDivisibleBy7( num / 10 - 2 *            ( num - num / 10 * 10 ) );} // Driver codeint main(){    int num = 616;    if( isDivisibleBy7(num ) )        cout << "Divisible" ;    else        cout << "Not Divisible" ;    return 0;} // This code is contributed by rathbhupendra
// A Program to check whether a number is divisible by 7#include <stdio.h> int isDivisibleBy7( int num ){    // If number is negative, make it positive    if( num < 0 )        return isDivisibleBy7( -num );     // Base cases    if( num == 0 || num == 7 )        return 1;    if( num < 10 )        return 0;     // Recur for ( num / 10 - 2 * num % 10 )    return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) );} // Driver program to test above functionint main(){    int num = 616;    if( isDivisibleBy7(num ) )        printf( "Divisible" );    else        printf( "Not Divisible" );    return 0;}
// Java program to check whether a number is divisible by 7import java.io.*; class GFG{    // Function to check whether a number is divisible by 7    static boolean isDivisibleBy7(int num)    {        // If number is negative, make it positive        if( num < 0 )            return isDivisibleBy7( -num );          // Base cases        if( num == 0 || num == 7 )            return true;        if( num < 10 )            return false;          // Recur for ( num / 10 - 2 * num % 10 )        return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) );    }         // Driver program    public static void main (String[] args)    {        int num = 616;        if(isDivisibleBy7(num))            System.out.println("Divisible");        else            System.out.println("Not Divisible");    }} // Contributed by Pramod Kumar
# Python program to check whether a number is divisible by 7 # Function to check whether a number is divisible by 7def isDivisibleBy7(num) :         # If number is negative, make it positive    if num < 0 :        return isDivisibleBy7( -num )     # Base cases    if( num == 0 or num == 7 ) :        return True         if( num < 10 ) :        return False             # Recur for ( num / 10 - 2 * num % 10 )    return isDivisibleBy7( num // 10 - 2 * ( num - num // 10 * 10 ) )     # Driver programnum = 616if(isDivisibleBy7(num)) :    print ("Divisible")else :    print ("Not Divisible") # This code is contributed by Nikita Tiwari
// C# program to check whether a// number is divisible by 7using System; class GFG {         // Function to check whether a    // number is divisible by 7    static bool isDivisibleBy7(int num)    {                 // If number is negative,        // make it positive        if( num < 0 )            return isDivisibleBy7(-num);         // Base cases        if( num == 0 || num == 7 )            return true;        if( num < 10 )            return false;         // Recur for ( num / 10 - 2 * num % 10 )        return isDivisibleBy7(num / 10 - 2 *                             ( num - num / 10 * 10 ));    }         // Driver Code    public static void Main ()    {        int num = 616;        if(isDivisibleBy7(num))            Console.Write("Divisible");        else            Console.Write("Not Divisible");    }} // This code is contributed by Nitin Mittal.
<?php// PHP Program to check whether// a number is divisible by 7 // Function to check whether a// number is divisible by 7function isDivisibleBy7( $num ){         // If number is negative,    // make it positive    if( $num < 0 )        return isDivisibleBy7( -$num );     // Base cases    if( $num == 0 || $num == 7 )        return 1;    if( $num < 10 )        return 0;     // Recur for ( num / 10 - 2 * num % 10 )    return isDivisibleBy7($num / 10 - 2 *                         ($num - $num / 10 * 10 ) );}     // Driver Code    $num = 616;    if( isDivisibleBy7($num )>=0 )        echo("Divisible");    else        echo("Not Divisible");     // This code is contributed by vt_m.?>
<script> // js Program to check whether// a number is divisible by 7 // Function to check whether a// number is divisible by 7function isDivisibleBy7( num ){         // If number is negative,    // make it positive    if( num < 0 )        return isDivisibleBy7( -num );     // Base cases    if( num == 0 || num == 7 )        return 1;    if( num < 10 )        return 0;     // Recur for ( num / 10 - 2 * num % 10 )    return isDivisibleBy7(num / 10 - 2 *                         (num - num / 10 * 10 ) );}     // Driver Code    let num = 616;    if( isDivisibleBy7(num )>=0 )        document.write("Divisible");    else        document.write("Not Divisible");  // This code is contributed by sravan kumar </script>
Divisible
Auxiliary Space: O(1)
How does this work? Let ‘b’ be the last digit of a number ‘n’ and let ‘a’ be the number we get when we split off ‘b’. The representation of the number may also be multiplied by any number relatively prime to the divisor without changing its divisibility. After observing that 7 divides 21, we can perform the following: 
 10.a + b 
after multiplying by 2, this becomes  
 20.a + 2.b 
and then  
 21.a - a + 2.b 
Eliminating the multiple of 21 gives  
 -a + 2b
and multiplying by -1 gives  
 a - 2b
Method: To check given number is divisible by 7 or not by using modulo division operator “%”.  
Python3
Javascript
# Python code# To check whether the given number is divisible by 7 or not #inputn=371# the above input can also be given as n=input() -> taking input from user# finding given number is divisible by 7 or notif int(n)%7==0:  print("divisible")else:  print("Not divisible")     # this code is contributed by gangarajula laxmi
<script>       // JavaScript code for the above approach        // To check whether the given number is divisible by 7 or not        //input       let n = 371       // the above input can also be given as n=input() -> taking input from user       // finding given number is divisible by 7 or not       if (n % 7 == 0)           document.write("divisible")       else           document.write("Not divisible")      // This code is contributed by Potta Lokesh   </script>
divisible
Method: Checking given number is divisible by 7 or not using modulo division.
C++
Java
C#
// C++ program to check if given number is divisible by 7 or// not using modulo division #include <iostream>using namespace std; int main() {      // input number    int num = 371;    // checking if the given number is divisible by 7 or not    // using modulo division operator if the output of num%7    // is equal to 0 then given number is divisible by 7    // otherwise not divisible by 7    if (num % 7 == 0) {        cout << " divisible";    }    else {        cout << " not divisible";    }    return 0;}  // this code is contributed by gangarajula laxmi
// java program to check if given number is divisible by 7 or// not using modulo division import java.io.*; class GFG {    public static void main (String[] args) {      // input number      int num=371;       // checking if the given number is divisible by 7 or not    // using modulo division operator if the output of num%7    // is equal to 0 then given number is divisible by 7    // otherwise not divisible by 7       if (num % 7 == 0) {        System.out.println(" divisible");    }    else {        System.out.println(" not divisible");    }         }}  // this code is contributed by gangarajula laxmi
// C# program to check if given number is divisible by 7 or// not using modulo divisionusing System; class GFG {  public static void Main(string[] args)  {    // input number    int num = 371;     // checking if the given number is divisible by 7 or    // not    // using modulo division operator if the output of    // num%7 is equal to 0 then given number is    // divisible by 7 otherwise not divisible by 7    if (num % 7 == 0) {      Console.WriteLine(" divisible");    }    else {      Console.WriteLine(" not divisible");    }  }} // This code is contributed by phasing17
 
There are other interesting methods to check divisibility by 7 and other numbers. See following Wiki page for details.References: http://en.wikipedia.org/wiki/Divisibility_rulePlease write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
nitin mittal
vt_m
rathbhupendra
sravankumar8128
subhammahato348
amartyaghoshgfg
laxmigangarajula03
lokeshpotta20
phasing17
divisibility
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.