Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Tutorials Student Jobs Courses Sign In

Related Articles Save for later

Check divisibility by 7
Difficulty Level : Easy ● Last Updated : 17 Jun, 2019

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.

Divisibilit y 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 digit s. 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++

WHAT’S NEW

Data Structures and


Algorithms – Self Paced
Course
View Details

Ad-Free Experience –
GeeksforGeeks Premium
 // A Program to check whether a number is divisible by 7 View Details
#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 code
int main()
{
int num = 616;
if( isDivisibleBy7(num ) )
cout << "Divisible" ;
else
cout << "Not Divisible" ;
return 0;
}

// This code is contributed by rathbhupendra

C
MOST POPULAR IN
MATHEMATICAL
 // A Program to check whether a number is divisible by 7
#include <stdio.h>
Counting Sort

int isDivisibleBy7( int num )
 { Program for Decimal to Binary
// If number is negative, make it positive
Conversion
 if( num < 0 )
return isDivisibleBy7( -num );

// Base cases
Program to find GCD or HCF of two
if( num == 0 || num == 7 ) numbers
return 1;
if( num < 10 )
return 0;
Merge two sorted arrays

// Recur for ( num / 10 - 2 * num % 10 ) Prime Numbers


return isDivisibleBy7( num / 10 - 2 * ( num - num / 1
}

// Driver program to test above function


int main()
{
int num = 616;
if( isDivisibleBy7(num ) )
printf( "Divisible" );
else
printf( "Not Divisible" );
return 0;
}

Java

 // Java program to check whether a number is divisible by


import java.io.*;

class GFG
 {
// Function to check whether a number is divisible by
 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 ) MOST VISITED IN SCHOOL


}
return isDivisibleBy7( num / 10 - 2 * ( num - num
PROGRAMMING
// Driver program Object Oriented Programming in C++
public static void main (String[] args)
{
int num = 616; Arrays in C/C++
if(isDivisibleBy7(num))
System.out.println("Divisible");
else Constructors in C++
System.out.println("Not Divisible");
} Reverse a string in Java
}

// Contributed by Pramod Kumar Inheritance in C++

P ython

 # Python program to check whether a number is divisible b

 # Function to check whether a number is divisible by 7


def 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 / 1

# Driver program
num = 616
if(isDivisibleBy7(num)) :
print "Divisible"
else :
print "Not Divisible"

# This code is contributed by Nikita Tiwari

C#

 // C# program to check whether a


// number is divisible by 7
 using 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
// PHP Program to check whether
 // a number is divisible by 7

 // Function to check whether a


// number is divisible by 7
 function 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.


?>

Output :

Divisible

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 of f ‘b’.

The representation of the number may also be multiplied by any

number relatively prime to the divisor without changing it s

divisibilit y. Af ter obser ving that 7 divides 21, we can per form the

following :

10.a + b

af ter 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

There are other interesting methods to check divisibilit y by 7 and

other numbers. See following Wiki page for details.

References :

http://en.wikipedia.org/wiki/Divisibilit y_rule

Please write comment s if you find anything incorrect, or you want

to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the

impor tant DS A concept s with the DS A Self Paced Course at a

student-friendly price and become industr y ready.

 Like 0

 Previous Next 
To check whether a Program to print all the
large number is numbers divisible by 3
divisible by 7 and 5 for a given
number

RECOMMENDED ARTICLES Page : 1 2 3

Check divisibility in a Sub-string Divisibility


01 05
binary stream by 3 Queries
16, Feb 17 04, Aug 16

To check divisibility
02 of any large number Sub-string Divisibility
by 999 06 by 11 Queries
26, Jun 17 08, Aug 16

Check if a large
03
number is divisibility Divisibility by 12 for a
07
by 15 large number
02, Nov 17 23, Jan 18

Check the divisibility


04 of Hexadecimal Repeated Unit
numbers 08 Divisibility
01, Nov 19 19, Apr 18

Ar ticle Contributed By :

GeeksforGeeks

Vote for di culty

Current di culty : Easy

Easy Normal Medium Hard Expert

Improved By : nitin mittal, vt_m, rathbhupendra


Article Tags : divisibility , Mathematical , School Programming
Practice Tags : Mathematical

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

Company Learn Practice Contribute


About Us Algorithms Courses Write an Article
5th Floor, A-118,
Sector-136, Noida, Uttar Pradesh - 201305 Careers Data Structures Company-wise Write Interview Experience
feedback@geeksforgeeks.org Privacy Policy Languages Topic-wise Internships
Contact Us CS Subjects How to begin? Videos
Copyright Policy Video Tutorials

@geeksforgeeks , Some rights reserved

You might also like