You are given an n-digit large number, you have to check whether it is divisible by 7.

A (r+1)-digit integer n whose digital form is **(ar ar-1 ar-2….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 3-digit 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 7Explanation: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 7Explanation: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.

## C++

`// C++ code to check divisibility of a` `// given large number by 7` `#include<bits/stdc++.h>` `using` `namespace` `std;` `int` `isdivisible7(` `char` `num[])` `{` ` ` `int` `n = ` `strlen` `(num), gSum;` ` ` `if` `(n == 0 && num[0] == ` `'\n'` `)` ` ` `return` `1;` ` ` `// Append required 0s at the beginning.` ` ` `if` `(n % 3 == 1) {` ` ` `strcat` `(num, ` `"00"` `);` ` ` `n += 2;` ` ` `}` ` ` `else` `if` `(n % 3 == 2) {` ` ` `strcat` `(num, ` `"0"` `);` ` ` `n++;` ` ` `}` ` ` `// add digits in group of three in gSum` ` ` `int` `i, GSum = 0, p = 1;` ` ` `for` `(i = n - 1; i >= 0; i--) {` ` ` `// group saves 3-digit group` ` ` `int` `group = 0;` ` ` `group += num[i--] - ` `'0'` `;` ` ` `group += (num[i--] - ` `'0'` `) * 10;` ` ` `group += (num[i] - ` `'0'` `) * 100;` ` ` `gSum = gSum + group * p;` ` ` `// generate alternate series of plus` ` ` `// and minus` ` ` `p *= (-1);` ` ` `}` ` ` `return` `(gSum % 7 == 0);` `}` `// Driver code` `int` `main()` `{` ` ` `// Driver method` ` ` `char` `num[] = ` `"8955795758"` `;` ` ` `if` `(isdivisible7(num))` ` ` `cout << ` `"Divisible by 7"` `;` ` ` `else` ` ` `cout << ` `"Not Divisible by 7"` `;` ` ` `return` `0;` `}` `// This code is contributed` `// by Akanksha Rai` |

## C

`// C code to check divisibility of a` `// given large number by 7` `#include <stdio.h>` `#include <string.h>` `int` `isdivisible7(` `char` `num[])` `{` ` ` `int` `n = ` `strlen` `(num), gSum;` ` ` `if` `(n == 0 && num[0] == ` `'\n'` `)` ` ` `return` `1;` ` ` `// Append required 0s at the beginning.` ` ` `if` `(n % 3 == 1) {` ` ` `strcat` `(num, ` `"00"` `);` ` ` `n += 2;` ` ` `}` ` ` `else` `if` `(n % 3 == 2) {` ` ` `strcat` `(num, ` `"0"` `);` ` ` `n++;` ` ` `}` ` ` `// add digits in group of three in gSum` ` ` `int` `i, GSum = 0, p = 1;` ` ` `for` `(i = n - 1; i >= 0; i--) {` ` ` `// group saves 3-digit group` ` ` `int` `group = 0;` ` ` `group += num[i--] - ` `'0'` `;` ` ` `group += (num[i--] - ` `'0'` `) * 10;` ` ` `group += (num[i] - ` `'0'` `) * 100;` ` ` `gSum = gSum + group * p;` ` ` `// generate alternate series of plus` ` ` `// and minus` ` ` `p *= (-1);` ` ` `}` ` ` `return` `(gSum % 7 == 0);` `}` `// Driver code` `int` `main()` `{` ` ` `// Driver method` ` ` `char` `num[] = ` `"8955795758"` `;` ` ` `if` `(isdivisible7(num))` ` ` `printf` `(` `"Divisible by 7"` `);` ` ` `else` ` ` `printf` `(` `"Not Divisible by 7"` `);` ` ` `return` `0;` `}` |

## Java

`// Java code to check divisibility of a given large number by 7` `class` `Test {` ` ` `// Method to check divisibility` ` ` `static` `boolean` `isDivisible7(String num)` ` ` `{` ` ` `int` `n = num.length();` ` ` `if` `(n == ` `0` `&& num.charAt(` `0` `) == ` `'0'` `)` ` ` `return` `true` `;` ` ` `// Append required 0s at the beginning.` ` ` `if` `(n % ` `3` `== ` `1` `)` ` ` `num = ` `"00"` `+ num;` ` ` `if` `(n % ` `3` `== ` `2` `)` ` ` `num = ` `"0"` `+ num;` ` ` `n = num.length();` ` ` `// add digits in group of three in gSum` ` ` `int` `gSum = ` `0` `, p = ` `1` `;` ` ` `for` `(` `int` `i = n - ` `1` `; i >= ` `0` `; i--) {` ` ` `// group saves 3-digit group` ` ` `int` `group = ` `0` `;` ` ` `group += num.charAt(i--) - ` `'0'` `;` ` ` `group += (num.charAt(i--) - ` `'0'` `) * ` `10` `;` ` ` `group += (num.charAt(i) - ` `'0'` `) * ` `100` `;` ` ` `gSum = gSum + group * p;` ` ` `// generate alternate series of plus and minus` ` ` `p = p * -` `1` `;` ` ` `}` ` ` `// calculate result till 3 digit sum` ` ` `return` `(gSum % ` `7` `== ` `0` `);` ` ` `}` ` ` `// Driver method` ` ` `public` `static` `void` `main(String args[])` ` ` `{` ` ` `String num = ` `"8955795758"` `;` ` ` `System.out.println(isDivisible7(num) ? ` `"Divisible by 7"` `: ` `"Not Divisible by 7"` `);` ` ` `}` `}` |

## Python3

`# Python 3 code to check divisibility` `# of a given large number by 7` `def` `isdivisible7(num):` ` ` `n ` `=` `len` `(num)` ` ` `if` `(n ` `=` `=` `0` `and` `num[` `0` `] ` `=` `=` `'\n'` `):` ` ` `return` `1` ` ` `# Append required 0s at the beginning.` ` ` `if` `(n ` `%` `3` `=` `=` `1` `) :` ` ` `num ` `=` `str` `(num) ` `+` `"00"` ` ` `n ` `+` `=` `2` ` ` ` ` `elif` `(n ` `%` `3` `=` `=` `2` `) :` ` ` `num ` `=` `str` `(num) ` `+` `"0"` ` ` `n ` `+` `=` `1` ` ` `# add digits in group of three in gSum` ` ` `GSum ` `=` `0` ` ` `p ` `=` `1` ` ` `for` `i ` `in` `range` `(n ` `-` `1` `, ` `-` `1` `, ` `-` `1` `) :` ` ` `# group saves 3-digit group` ` ` `group ` `=` `0` ` ` `group ` `+` `=` `ord` `(num[i]) ` `-` `ord` `(` `'0'` `)` ` ` `i ` `-` `=` `1` ` ` `group ` `+` `=` `(` `ord` `(num[i]) ` `-` `ord` `(` `'0'` `)) ` `*` `10` ` ` `i ` `-` `=` `1` ` ` `group ` `+` `=` `(` `ord` `(num[i]) ` `-` `ord` `(` `'0'` `)) ` `*` `100` ` ` `GSum ` `=` `GSum ` `+` `group ` `*` `p` ` ` `# generate alternate series of` ` ` `# plus and minus` ` ` `p ` `*` `=` `(` `-` `1` `)` ` ` `return` `(GSum ` `%` `7` `=` `=` `0` `)` `# Driver code` `if` `__name__ ` `=` `=` `"__main__"` `:` ` ` ` ` `num ` `=` `"8955795758"` ` ` `if` `(isdivisible7(num)):` ` ` `print` `(` `"Divisible by 7"` `)` ` ` `else` `:` ` ` `print` `(` `"Not Divisible by 7"` `)` `# This code is contributed by ChitraNayal` |

## C#

`// C# code to check divisibility of a` `// given large number by 7` `using` `System;` `class` `GFG {` ` ` `// Method to check divisibility` ` ` `static` `bool` `isDivisible7(String num)` ` ` `{` ` ` `int` `n = num.Length;` ` ` `if` `(n == 0 && num[0] == ` `'0'` `)` ` ` `return` `true` `;` ` ` `// Append required 0s at the beginning.` ` ` `if` `(n % 3 == 1)` ` ` `num = ` `"00"` `+ num;` ` ` `if` `(n % 3 == 2)` ` ` `num = ` `"0"` `+ num;` ` ` `n = num.Length;` ` ` `// add digits in group of three in gSum` ` ` `int` `gSum = 0, p = 1;` ` ` `for` `(` `int` `i = n - 1; i >= 0; i--) {` ` ` `// group saves 3-digit group` ` ` `int` `group` `= 0;` ` ` `group` `+= num[i--] - ` `'0'` `;` ` ` `group` `+= (num[i--] - ` `'0'` `) * 10;` ` ` `group` `+= (num[i] - ` `'0'` `) * 100;` ` ` `gSum = gSum + ` `group` `* p;` ` ` `// generate alternate series` ` ` `// of plus and minus` ` ` `p = p * -1;` ` ` `}` ` ` `// calculate result till 3 digit sum` ` ` `return` `(gSum % 7 == 0);` ` ` `}` ` ` `// Driver code` ` ` `static` `public` `void` `Main()` ` ` `{` ` ` `String num = ` `"8955795758"` `;` ` ` `// Function calling` ` ` `Console.WriteLine(isDivisible7(num) ? ` `"Divisible by 7"` `: ` `"Not Divisible by 7"` `);` ` ` `}` `}` `// This code is contributed by Ajit.` |

## PHP

`<?php` `// PHP code to check divisibility of` `// a given large number by 7` `// Function to check divisibility` `function` `isDivisible7(` `$num` `)` `{` ` ` `$n` `= ` `strlen` `(` `$num` `) ;` ` ` `if` `(` `$n` `== 0 && ` `$num` `[0] == ` `'0'` `)` ` ` `return` `true;` ` ` `// Append required 0s at the beginning.` ` ` `if` `(` `$n` `% 3 == 1)` ` ` `$num` `= ` `"00"` `. ` `$num` `;` ` ` `if` `(` `$n` `% 3 == 2)` ` ` `$num` `= ` `"0"` `. ` `$num` `;` ` ` `$n` `= ` `strlen` `(` `$num` `);` ` ` `// add digits in group of three in gSum` ` ` `$gSum` `= 0 ;` ` ` `$p` `= 1;` ` ` `for` `(` `$i` `= ` `$n` `- 1; ` `$i` `>= 0; ` `$i` `--)` ` ` `{` ` ` `// group saves 3-digit group` ` ` `$group` `= 0;` ` ` `$group` `+= ` `$num` `[` `$i` `--] - ` `'0'` `;` ` ` `$group` `+= (` `$num` `[` `$i` `--] - ` `'0'` `) * 10;` ` ` `$group` `+= (` `$num` `[` `$i` `] - ` `'0'` `) * 100;` ` ` `$gSum` `= ` `$gSum` `+ ` `$group` `* ` `$p` `;` ` ` ` ` `// generate alternate series` ` ` `// of plus and minus` ` ` `$p` `= ` `$p` `* -1;` ` ` `}` ` ` `// calculate result till 3 digit sum` ` ` `return` `(` `$gSum` `% 7 == 0);` `}` `// Driver Code` `$num` `= ` `"8955795758"` `;` `echo` `(isDivisible7(` `$num` `) ?` ` ` `"Divisible by 7"` `:` ` ` `"Not Divisible by 7"` `);` `// This code is contributed by Ryuga` `?>` |

## Javascript

`<script>` `// Javascript code to check divisibility of` `// a given large number by 7` `// Function to check divisibility` `function` `isDivisible7(num)` `{` ` ` `let n = num.length;` ` ` ` ` `if` `(n == 0 && num[0] == ` `'0'` `)` ` ` `return` `true` `;` ` ` `// Append required 0s at the beginning.` ` ` `if` `(n % 3 == 1)` ` ` `num = ` `"00"` `+ num;` ` ` `if` `(n % 3 == 2)` ` ` `num = ` `"0"` `+ num;` ` ` ` ` `n = num.length;` ` ` `// Add digits in group of three in gSum` ` ` `gSum = 0 ;` ` ` `let p = 1;` ` ` ` ` `for` `(let i = n - 1; i >= 0; i--)` ` ` `{` ` ` ` ` `// Group saves 3-digit group` ` ` `group = 0;` ` ` `group += num[i--] - ` `'0'` `;` ` ` `group += (num[i--] - ` `'0'` `) * 10;` ` ` `group += (num[i] - ` `'0'` `) * 100;` ` ` `gSum = gSum + group * p;` ` ` ` ` `// Generate alternate series` ` ` `// of plus and minus` ` ` `p = p * -1;` ` ` `}` ` ` `// Calculate result till 3 digit sum` ` ` `return` `(gSum % 7 == 0);` `}` `// Driver Code` `let num = ` `"8955795758"` `;` `document.write(isDivisible7(num) ?` ` ` `"Divisible by 7"` `:` ` ` `"Not Divisible by 7"` `);` `// This code is contributed by _saurabh_jaiswal` ` ` `</script>` |

**Output:**

Divisible by 7

This article is contributed by **Sruti Rai**.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 write comments 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 important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.