Given a number N, the task is to check if it is divisible by 7 or not.
Note: You are not allowed to use the modulo operator, floating point arithmetic is also not allowed.
Naive approach: 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++
#include <bits/stdc++.h>
using namespace std;
int isDivisibleBy7( int num )
{
if( num < 0 )
return isDivisibleBy7( -num );
if( num == 0 || num == 7 )
return 1;
if( num < 10 )
return 0;
return isDivisibleBy7( num / 10 - 2 *
( num - num / 10 * 10 ) );
}
int main()
{
int num = 616;
if( isDivisibleBy7(num ) )
cout << "Divisible" ;
else
cout << "Not Divisible" ;
return 0;
}
|
C
#include <stdio.h>
int isDivisibleBy7( int num )
{
if( num < 0 )
return isDivisibleBy7( -num );
if( num == 0 || num == 7 )
return 1;
if( num < 10 )
return 0;
return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) );
}
int main()
{
int num = 616;
if( isDivisibleBy7(num ) )
printf( "Divisible" );
else
printf( "Not Divisible" );
return 0;
}
|
Java
import java.io.*;
class GFG
{
static boolean isDivisibleBy7(int num)
{
if( num < 0 )
return isDivisibleBy7( -num );
if( num == 0 || num == 7 )
return true;
if( num < 10 )
return false;
return isDivisibleBy7( num / 10 - 2 * ( num - num / 10 * 10 ) );
}
public static void main (String[] args)
{
int num = 616;
if(isDivisibleBy7(num))
System.out.println("Divisible");
else
System.out.println("Not Divisible");
}
}
|
Python3
def isDivisibleBy7(num) :
if num < 0 :
return isDivisibleBy7( -num )
if( num == 0 or num == 7 ) :
return True
if( num < 10 ) :
return False
return isDivisibleBy7( num // 10 - 2 * ( num - num // 10 * 10 ) )
num = 616
if(isDivisibleBy7(num)) :
print ("Divisible")
else :
print ("Not Divisible")
|
C#
using System;
class GFG {
static bool isDivisibleBy7(int num)
{
if( num < 0 )
return isDivisibleBy7(-num);
if( num == 0 || num == 7 )
return true;
if( num < 10 )
return false;
return isDivisibleBy7(num / 10 - 2 *
( num - num / 10 * 10 ));
}
public static void Main ()
{
int num = 616;
if(isDivisibleBy7(num))
Console.Write("Divisible");
else
Console.Write("Not Divisible");
}
}
|
PHP
<?php
function isDivisibleBy7( $num )
{
if( $num < 0 )
return isDivisibleBy7( -$num );
if( $num == 0 || $num == 7 )
return 1;
if( $num < 10 )
return 0;
return isDivisibleBy7($num / 10 - 2 *
($num - $num / 10 * 10 ) );
}
$num = 616;
if( isDivisibleBy7($num )>=0 )
echo("Divisible");
else
echo("Not Divisible");
?>
|
Javascript
<script>
function isDivisibleBy7( num )
{
if( num < 0 )
return isDivisibleBy7( -num );
if( num == 0 || num == 7 )
return 1;
if( num < 10 )
return 0;
return isDivisibleBy7(num / 10 - 2 *
(num - num / 10 * 10 ) );
}
let num = 616;
if( isDivisibleBy7(num )>=0 )
document.write("Divisible");
else
document.write("Not Divisible");
</script>
|
Time Complexity: O(log n)
Auxiliary Space: O(log n)
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 the modulo division operator “%”.
C++
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n = 371;
if (n % 7 == 0) {
cout << ("divisible");
}
else {
cout << ("Not divisible");
}
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int n = 371;
if ((int)n % 7 == 0) {
System.out.print("divisible");
}
else {
System.out.print("Not divisible");
}
}
}
|
Python3
n=371
if int(n)%7==0:
print("divisible")
else:
print("Not divisible")
|
C#
using System;
public class GFG {
static public void Main()
{
int n = 371;
if ((int)n % 7 == 0) {
Console.Write("divisible");
}
else {
Console.Write("Not divisible");
}
}
}
|
Javascript
<script>
let n = 371
if (n % 7 == 0)
document.write("divisible")
else
document.write("Not divisible")
</script>
|
PHP
<?php
$n=371;
if ($n % 7 == 0)
{
echo "Divisible";
}
else
{
echo "Not divisible";
}
?>
|
Time complexity: O(1) because it is performing constant operations
Auxiliary space: O(1)
Method: Checking the divisibility using the arithmetic(/) operator
C++
#include <iostream>
using namespace std;
int main() {
int n = 616;
int quo = n/7;
if((quo*7) == n){
cout << "Divisible";
}
else{
cout << "Not Divisible";
}
return 0;
}
|
Java
import java.io.*;
class Gfg {
public static void main(String[] args) {
int n = 616;
int quo = n/7;
if((quo*7) == n){
System.out.println("Divisible");
}
else{
System.out.println("Not Divisible");
}
}
}
|
Python3
n = 616
quo = int(n/7)
if quo*7 == n:
print("Divisible")
else:
print("Not Divisible")
|
C#
using System;
using System.Collections.Generic;
public class Gfg
{
public static void Main(string[] args)
{
int n = 616;
int quo = n/7;
if((quo*7) == n){
Console.WriteLine("Divisible");
}
else{
Console.WriteLine("Not Divisible");
}
}
}
|
Javascript
let n = 616;
let quo = Math.floor(n/7);
if((quo*7) == n){
console.log("Divisible");
}
else{
console.log("Not Divisible");
}
|
Time complexity : O(1)
Space complexity : O(1)
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_rule
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Please Login to comment...