Given a number as string n, return the remainder of the number when it is divided by 7.
Examples :
Input: n = "5"
Output: 5
Explanation: Since 5 is less than 7, the remainder when divided by 7 is 5.Input: n = "8"
Output: 1
Explanation: 8 divided by 7 leaves remainder 1.
Table of Content
Using Digit by Digit Modulo - O(n) Time O(1) Space
The idea is to process the number digit by digit and keep updating the remainder using modulo operation. For each digit, we multiply the current remainder by 10, add the digit, and take
% 7. This avoids forming the full number, so it works efficiently even for very large inputs without overflow.
#include <iostream>
using namespace std;
// function to find remainder
int remainderWith7(string &n)
{
int res = 0;
for (int i = 0; i < n.size(); i++)
{
// Add current digit and keep remainder modulo 7
res = (res * 10 + (n[i] - '0')) % 7;
}
return res;
}
int main()
{
string n = "8";
cout << remainderWith7(n) << endl;
return 0;
}
public class GfG {
// function to find remainder
static int remainderWith7(String n)
{
int res = 0;
for (int i = 0; i < n.length(); i++)
{
res = (res * 10 + (n.charAt(i) - '0')) % 7;
}
return res;
}
// Driver Code
public static void main(String[] args)
{
String n = "8";
System.out.println(remainderWith7(n));
}
}
# function to find remainder
def remainderWith7(n):
res = 0
for i in range(len(n)):
res = (res * 10 + (int(n[i]))) % 7
return res
# Driver Code
if __name__ == "__main__":
n = "8"
print(remainderWith7(n))
using System;
public class GfG
{
// function to find remainder
public static int remainderWith7(string n)
{
int res = 0;
for (int i = 0; i < n.Length; i++)
{
res = (res * 10 + (n[i] - '0')) % 7;
}
return res;
}
// Driver Code
public static void Main()
{
string n = "8";
Console.WriteLine(remainderWith7(n));
}
}
// function to find remainder
function remainderWith7(n) {
let res = 0;
for (let i = 0; i < n.length; i++) {
res = (res * 10 + (n[i] - '0')) % 7;
}
return res;
}
// Driver Code
let n = "8";
console.log(remainderWith7(n));
Output
1
Using Cyclic Pattern Method - O(n) Time O(1) Space
The idea is to use the repeating pattern of powers of 10 modulo 7: [1, 3, 2, -1, -3, -2]. We traverse the number from right to left, multiply each digit with the corresponding value from this pattern, and keep adding the result modulo 7. Since the pattern repeats every 6 digits, we cycle through it. Finally, we adjust the result to ensure it is positive. This method avoids large number formation and uses mathematical optimization.
Consider : n = "8"
- Start with res = 0, idx = 0 and series = [1, 3, 2, -1, -3, -2]
- Process last digit 8 -> res = (0 + 8 × 1) % 7 = 1
- Update idx = 1, loop ends
- Final res = 1
#include <iostream>
using namespace std;
// function to find remainder
int remainderWith7(string &n)
{
// Repeating pattern of powers of 10 modulo 7
int series[] = {1, 3, 2, -1, -3, -2};
int idx = 0;
int res = 0;
for (int i = n.size() - 1; i >= 0; i--)
{
int digit = n[i] - '0';
res = (res + digit * series[idx]) % 7;
idx = (idx + 1) % 6;
}
// Convert negative remainder to positive
if (res < 0)
res = (res + 7) % 7;
return res;
}
// Driver Code
int main()
{
string n = "8";
cout << remainderWith7(n) << endl;
return 0;
}
public class GfG {
// function to find remainder
public static int remainderWith7(String n) {
int[] series = {1, 3, 2, -1, -3, -2};
int idx = 0;
int res = 0;
for (int i = n.length() - 1; i >= 0; i--) {
int digit = n.charAt(i) - '0';
res = (res + digit * series[idx]) % 7;
idx = (idx + 1) % 6;
}
if (res < 0)
res = (res + 7) % 7;
return res;
}
// Driver Code
public static void main(String[] args) {
String n = "8";
System.out.println(remainderWith7(n));
}
}
def remainderWith7(n):
series = [1, 3, 2, -1, -3, -2]
idx = 0
res = 0
for i in range(len(n) - 1, -1, -1):
digit = int(n[i])
res = (res + digit * series[idx]) % 7
idx = (idx + 1) % 6
if res < 0:
res = (res + 7) % 7
return res
# Driver Code
if __name__ == "__main__":
n = "8"
print(remainderWith7(n))
using System;
public class GfG
{
// function to find remainder
public static int remainderWith7(string n)
{
int[] series = {1, 3, 2, -1, -3, -2};
int idx = 0;
int res = 0;
for (int i = n.Length - 1; i >= 0; i--)
{
int digit = n[i] - '0';
res = (res + digit * series[idx]) % 7;
idx = (idx + 1) % 6;
}
if (res < 0)
res = (res + 7) % 7;
return res;
}
// Driver Code
public static void Main()
{
string n = "8";
Console.WriteLine(remainderWith7(n));
}
}
// function to find remainder
function remainderWith7(n) {
const series = [1, 3, 2, -1, -3, -2];
let idx = 0;
let res = 0;
for (let i = n.length - 1; i >= 0; i--) {
let digit = parseInt(n[i]);
res = (res + digit * series[idx]) % 7;
idx = (idx + 1) % 6;
}
res = (res + 7) % 7;
return res;
}
// Driver Code
let n = "8";
console.log(remainderWith7(n));
Output
1