You are given an ndigit large number, you have to check whether it is divisible by 7.
A (r+1)digit integer n whose digital form is (ar ar1 ar2….
a2 a1 a0) is divisible by 7 if and only if the alternate series of numbers (a2 a1 a0) + (a5 a4 a3) – (a8 a7 a6) + … is divisible by 7.
The triplets of digits within parenthesis represent 3digit number in digital form.
The given number n can be written as a sum of powers of 1000 as follows.
n= (a2 a1 a0) + (a5 a4 a3)*1000 + (a8 a7 a6)*(1000*1000) +….
As 1000 = (1)(mod 7), 1000 as per congruence relation.
For a positive integer n, two numbers a and b are said to be congruent modulo n, if their difference
(a – b) is an integer multiple of n (that is, if there is an integer k such that a – b = kn).
This congruence relation is typically considered when a and b are integers, and is denoted
Hence we can write:
n = { (a2a1a0) + (a5a4a3)* (1) + (a8a7a6)* (1)*(1)+…..}(mod 7),
Thus n is divisible by 7 if and if only if the series is divisible by 7.
Examples :
Input : 8955795758 Output : Divisible by 7 Explanation: We express the number in terms of triplets of digits as follows. (008)(955)(795)(758) Now, 758 795 + 955 – 8 = 910, which is divisible by 7 Input : 100000000000 Output : Not Divisible by 7 Explanation:
We express the number in terms of triplets
of digits as follows.
(100)(000)(000)(000)
Now, 000 000 + 000 – 100 = 100, which is
not divisible by 7
Note that the number of digits in n may not be multiple of 3 . In that case we pas zero(s) on the left side of the remaining digits(s) after taking out all the triplets (from right side of n) to form the last triplet.
A simple and efficient method is to take input in form of string (make its length in form of 3*m by adding 0 to left of number if required) and then you have to add the digits in blocks of three from right to left until it become a 3 digit number to form an alternate series and check whether the series is divisible by 7 or not.
Here the program implementation to check divisibility of 7 is done.
Recommended: Please try your approach on {IDE} first, before moving on to the solution
System.out.println(isDivisible7(num) ? “Divisible by 7” : “Not Divisible by 7”); 

Console.WriteLine(isDivisible7(num) ? “Divisible by 7” : “Not Divisible by 7”); 

Output:
Divisible by 7
This article is contributed by Sruti Rai
Divisible by 7  Test of Divisibility by 7  Rules of Divisibility by 7
Divisible by 7 is discussed below:
We need to double the last digit of the number and then subtract it from the remaining number. If the result is divisible by 7, then the original number will also be divisible by 7.
Consider the following numbers which are divisible by 7, using the test of divisibility by 7: 133, 273, 329, 595, 672.
 (i) 133
 In the number 133, double the last digit of the number 3 is 6. 13 – 6 = 7 [Now we need to subtract it from the rest of the remaining number]
 Since 7 is divisible by 7.
 Hence, 133 is also divisible by 7.
(ii) 273In the number 273, double the last digit of the number 3 is 6. 27 – 6 = 21 [Now we need to subtract it from the rest of theremaining number]Since 21 is divisible by 7.Hence, 273 is also divisible by 7.
 (iii) 329
 (iv) 595
 (v) 672
 (i) 167
 (ii) 233
 (iii)297
In the number 329, double the last digit of the number9 is 18. 32 – 18 = 14 [Now we need to subtract it from therest of theremaining number]Since 14 is divisible by 7.Hence, 329is also divisible by 7.In the number 595, double the last digit of the number5 is 10. 59 – 10 = 49 [Now we need to subtract it from therest of theremaining number]Since 49 is divisible by 7.Hence, 595is also divisible by 7.In the number 672, double the last digit of the number2 is 4. 67 – 4 = 63 [Now we need to subtract it from therest of theremaining number]Since 63 is divisible by 7.Hence, 672is also divisible by 7.Consider the following numbers which are not divisible by 7, using the rules of divisibility by 7: 167, 233, 297, 305,317.In the number 167, double the last digit of the number 7 is 14. 16 – 14 = 2 [Now we need to subtract it from therest of theremaining number]Since 2 is not divisible by 7.Hence, 167 is also not divisible by 7.In the number 233, double the last digit of the number 3 is 6. 23 – 6 = 17 [Now we need to subtract it from therest of theremaining number]Since 17 is not divisible by 7.Hence, 233 is also not divisible by 7.In the number 297, double the last digit of the number 7 is 14. 29 – 14 = 15 [Now we need to subtract it from therest of theremaining number]Since 15 is not divisible by 7.Hence, 297 is also not divisible by 7.
(iv) 305In the number 305, double the last digit of the number 5 is 10. 30 – 10 = 20 [Now we need to subtract it from therest of theremaining number]Since 20 is not divisible by 7.Hence, 305 is also not divisible by 7.
(v) 317In the number 317, double the last digit of the number 7 is 14. 31 – 14 = 17 [Now we need to subtract it from therest of theremaining number]Since 17 is not divisible by 7.
Hence, 317is also not divisible by 7.
 ● Divisibility Rules.
 Properties of Divisibility.
 Divisible by 2.
 Divisible by 3.
 Divisible by 4.
 Divisible by 5.
 Divisible by 6.
 Divisible by 7.
 Divisible by 8.
 Divisible by 9.
 Divisible by 10.
 Problems on Divisibility Rules
 Worksheet on Divisibility Rules
 5th Grade Math Problems
 From Divisible by 7 to HOME PAGE
Divisibility Rules – 7
Mirror, mirror, on the wall,
who's the cleverest of them all?
The evil witch, Snow White, or her 7 friends
Stay tuned to see how this story ends
The witch has a notion she thinks is terrific
to make a potion that's also a soporific.
With Snow White in dreamland,
the evil witch can unveil her plan.
There's just one glitch,
a hitch for the witch
To avoid a oneway ticket to heaven,
Snow White leans on the Divisibility Rules for the number 7.
In an attempt to trick Snow White, the witch offers her a basket filled with 15, deliciouslooking apples.
Snow White doesn’t know the apples are laced with a sleeping potion, but she rejects them regardless.
Why? Because she can’t divide the 15 apples among the 7 dwarfs evenly, and she doesn't play favorites.
The witch is not discouraged. So the very next day, she returns. This time, she has a cart full of apples.
The witch doubts that Snow White can calculate such a large quotient quickly and will simply decide to accept the cart and its poisonous contents.
The witch proudly declares that she has 543 apples, more than the dwarves and Snow White can ever eat.
Again, Snow White refuses because she can’t divide the number of apples evenly into groups of 7.
How did she determine this so quickly?
Divisibility by 7
Snow White is a master of the divisibility rule for the number 7, so she doesn’t have to always rely on long division.
To check if a number is evenly divisible by 7:
Take the last digit of the number, double it
Then subtract the result from the rest of the number
If the resulting number is evenly divisible by 7, so is the original number.
Let’s try the trick on the number of apples in the cart, 543.
The last digit is 3, double that to make 6, subtract from 6 from the remaining digits. 54 minus 6 is equal to 48.
48’s not evenly divisible by 7, so 543 isn't evenly divisible by 7 either.
Let's check, just to make sure. 7 goes into 54 seven times.
Subtract 49 from 54, bring down the 3, 7 goes into 53 seven times, subtract 49 from 53, which leaves us with a remainder of 4. So we were right! 543 isn't evenly divisible by 7!
Foiled again. What's an evil witch to do?
Has Snow White simply outsmarted her?
The evil witch doesn't give up. She gathers all the apples in the kingdom, 2478 to be exact, and delivers them to Snow White.
Let’s see.Ok. The last digit is 8. Double it, and we get 16. Subtract 16 from 247. The difference is 231.
That’s still a big number, so we just do the same steps again. Double the last digit, that's equal to 2 and 23 minus 2 is equal to 21.
21 is evenly divisible by 7, so the ginormous pile of apples must also be evenly divisible by 7! 7 goes into 24 three times, subtract 21 from 24, bring down the 7, 7 goes into 37 five times.
Subtracting from 37 gives us 2 and 7 goes into 28 exactly 4 times.
Whaddya know? Snow White was correct! 2478 IS evenly divisible by 7!
While we were busy calculating,
77 pies are now ready and waiting.
Prepared by Snow White with love and care,
her pies are famous far, wide and everywhere.
And because she's so super sweet,
she offers the witch a pie that can't be beat.
Divisibility by 7
How can you tell whether a number is divisible by 7? Most everyone knows how to easily tell whether a number is divisible by 2, 3, 5, or 9. A few less know tricks for testing divisibility by 4, 6, 8, or 11. But not many people have ever seen a trick for testing divisibility by 7.
Here’s the trick. Remove the last digit from the number, double it, and subtract it from the first part of the number. Do this repeatedly until you get something you recognize as being divisible by 7 or not.
For example, start with 432. Split into 43 and 2. Subtract 4 from 43 to get 39. Since 39 isn’t divisible by 7, neither is 432.
For another example, start with 8631. Split into 863 and 1. Subtract 2 from 863 to get 861.
Now split 861 into Split into 86 and 1. Subtract 2 from 86. Maybe you recognize 84 as a multiple of 7. If not, double 4 and subtract from 8 to get 0, which is divisible by 7. Either way, we conclude that 8631 is divisible by 7.
Why 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. That says n = 10a + b. Now n is divisible by 7 if and only if n – 21b is divisible by 7. But n – 21b = 10(a – 2b) and this is divisible by 7 if and only if a – 2b is divisible by 7.
What about the remainder when you divide a number by 7? Here’s where the rule for 7 differs from the more familiar divisibility rules.
For example, a number is divisible by 3 if its digit sum is divisible by 3, and furthermore the remainder when a number is divided by 3 is the remainder when its digit sum is divided by 3.
But the divisibility rule for 7 does not give the remainder when a number is divided by 7. For a simple example, the divisibility rule for 31 terminates in 1, but the remainder with 31 is divided by 7 is 3.
Why doesn’t the divisibility rule for 7 give the remainder? It is true that 10a + b and (10a + b) – 21b have the same remainder when divided by 7. But then we factored this into 10(a 2b). It’s true that 10(a – 2b) is divisible by 7 if and only if (a – 2b) is divisible by 7, but if neither is divisible by 7 then they will leave different remainders.
Related posts
Divisibility by prime numbers under 50
A number is divisible by 2 if its last digit is also (i.e. 0,2,4,6 or 8).
A number is divisible by 3 if the sum of its digits is also. Example: 534: 5+3+4=12 and 1+2=3 so 534 is divisible by 3.
A number is divisible by 5 if the last digit is 5 or 0.
Most people know (only) those 3 rules. Here are my rules for divisibility by the PRIMES up to 50. Why only primes and not also composite numbers? A number is divisible by a composite if it is also divisible by all the prime factors (e.g.
is divisible by 21 if divisible by 3 AND by 7).
Small numbers are used in these worked examples, so you could have used a pocket calculator.
But my rules apply to any number of digits, whereas you cannot test a 30 or more digit number on your pocket calculator otherwise.
Test for divisibility by 7. Double the last digit and subtract it from the remaining leading truncated number. If the result is divisible by 7, then so was the original number. Apply this rule over and over again as necessary. Example: 826. Twice 6 is 12. So take 12 from the truncated 82. Now 8212=70. This is divisible by 7, so 826 is divisible by 7 also.
There are similar rules for the remaining primes under 50, i.e. 11,13,
17,19,23,29,31,37,41,43 and 47.
Test for divisibility by 11. Subtract the last digit from the remaining leading truncated number. If the result is divisible by 11, then so was the first number. Apply this rule over and over again as necessary. Example: 19151–> 19151 =1914 –>1914=187 –>187=11, so yes, 19151 is divisible by 11.
Test for divisibility by 13. Add four times the last digit to the remaining leading truncated number. If the result is divisible by 13, then so was the first number. Apply this rule over and over again as necessary. Example: 50661–>5066+4=5070–>507+0=507–>50+28=78 and 78 is 6*13, so 50661 is divisible by 13.
Test for divisibility by 17. Subtract five times the last digit from the remaining leading truncated number. If the result is divisible by 17, then so was the first number. Apply this rule over and over again as necessary. Example: 3978–>3975*8=357–>355*7=0. So 3978 is divisible by 17.
Test for divisibility by 19. Add two times the last digit to the remaining leading truncated number. If the result is divisible by 19, then so was the first number. Apply this rule over and over again as necessary. EG: 101156–>10115+2*6=10127–>1012+2*7=1026–>102+2*6=114 and 114=6*19, so 101156 is divisible by 19.
My original divisibilty webpage stopped here. However, I have had a number of mails asking for divisibility tests for larger primes, so I've extended the list up to 50.
Actually even with 37 most people cannot do the necessary mental arithmetic easily, because they cannot recognise even singledigit multiples of twodigit numbers on sight.
People are no longer taught the multiplication table up to 20*20 as I was as a child. Nowadays we are lucky if they know it up to 10*10.
Test for divisibility by 23. 3*23=69, ends in a 9, so ADD. Add 7 times the last digit to the remaining leading truncated number. If the result is divisible by 23, then so was the first number. Apply this rule over and over again as necessary. Example: 17043–>1704+7*3=1725–>172+7*5=207 which is 9*23, so 17043 is also divisible by 23.
Test for divisibility by 29. Add three times the last digit to the remaining leading truncated number. If the result is divisible by 29, then so was the first number. Apply this rule over and over again as necessary. Example: 15689–>1568+3*9=1595–>159+3*5=174–>17+3*4=29, so 15689 is also divisible by 29.
Test for divisibility by 31. Subtract three times the last digit from the remaining leading truncated number. If the result is divisible by 31, then so was the first number. Apply this rule over and over again as necessary. Example: 7998–>7993*8=775–>773*5=62 which is twice 31,
so 7998 is also divisible by 31.
Test for divisibility by 37. This is (slightly) more difficult, since it perforce uses a doubledigit multiplier, namely eleven. People can usually do single digit multiples of 11, so we can use the same technique still.
Subtract eleven times the last digit from the remaining leading truncated number. If the result is divisible by 37, then so was the first number. Apply this rule over and over again as necessary.
Example: 23384–>233811*4=2294–>22911*4=185 which is five times 37,
so 23384 is also divisible by 37.
Test for divisibility by 41. Subtract four times the last digit from the remaining leading truncated number. If the result is divisible by 41, then so was the first number. Apply this rule over and over again as necessary. Example: 30873–>30874*3=3075–>3074*5=287–>284*7=0, remainder is zero and so 30873 is also divisible by 41.
Test for divisibility by 43. Now it starts to get really difficult for most people, because the multiplier to be used is 13, and most people cannot recognise even single digit multiples of 13 at sight. You may want to make a little list of 13*N first.
Nevertheless, for the sake of completeness, we will use the same method.
Add thirteen times the last digit to the remaining leading truncated number. If the result is divisible by 43, then so was the first number. Apply this rule over and over again as necessary.
Example: 3182–>318+13*2=344–>34+13*4=86 which is recognisably twice 43, and so 3182 is also divisible by 43.
Update : Bill Malloy has pointed out that, since we are working to modulo43, instead of adding factor 13 times the last digit, we can subtract 30 times it, because 13+30=43.
Why didn't I think of that!!! 🙁
Finally, the Test for divisibility by 47. This too is difficult for most people, because the multiplier to be used is 14, and most people cannot recognise even single digit multiples of 14 at sight. You may want to make a little list of 14*N first.
Nevertheless, for the sake of completeness, we will use the same method.
Subtract fourteen times the last digit from the remaining leading truncated number. If the result is divisible by 47, then so was the first number. Apply this rule over and over again as necessary.
Example: 34827–>348214*7=3384–>33814*4=282–>2814*2=0 , remainder is zero and so 34827 is divisible by 47.
I've stopped here at the last prime below 50, for arbitrary but pragmatic reasons as explained above.
Other blogreaders (sadly even people from .edu domains, who should be able to do the elementary algebra themselves) have asked why I sometimes say ADD and for other primes say SUBTRACT, and ask where the apparently arbitrary factors come from. So let us do some algebra to show the method in my madness.
We have displayed the recursive divisibility test of number N as fM*r where f are the front digits of N, r is the rear digit of N and M is some multiplier. And we want to see if N is divisible by some prime P.
We need a method to work out the values of M.
What you do is to calculate (mentally) the smallest multiple of P which ends in a 9 or a 1. If it's a 9 we are going to ADD, Then we will use the leading digit(s) of the multiple +1 as our multiplier M.
If it's a 1 we are going to SUBTRACT later.
then we will use the leading digit(s) of the multiple as our multiplier M.
Example for P=17 : three times 17 is 51 which is the smallest multiple of 17 that ends in a 1 or 9. Since it's a 1 we are going to SUBTRACT later. The leading digit is a 5, so we are going to SUBTRACT five times the remainder r. The algorithm was stated above.
Now let's do the algebraic proof.
Writing N=10f+r, we can multiply by 5 (as shown in the example for 17), getting 5N=50f5r. Now we add 51f to both sides (because 51 was the smallest multiple of P=17 to end in a 1 or a 9), giving one f (which we want), so 51f5N=f5r.
Now if N is divisible by P (here P=17), we can substitute to get
51f5*17*x=f5r and rearrange the left side as 17*(3f5x)=f5r and therefore f5r is a multiple of P=17 also. Q.E.D.
Now go visit my blog please , or look at other interesting maths stuff 🙂
Index/Home  Impressum  Sitemap  Search 
Leave a Reply