Program to find remainder when large number is divided by r
Given a Number N, the task is to find the Remainder when N is divided by R (a two digit Number). The input of the Number may be very large.
Examples:
Input: N = 13589234356546756, R = 13 Output: 11 Input: N = 3435346456547566345436457867978, R = 17 Output: 13
- Get the digit of N one by one from left to right.
- For each digit, combine with next digit if its less than R.
- If the combination at any point reaches above R, take and store the Remainder.
- Repeat the above steps for all digits from left to right.
Below is the program that implements the above approach:
C++
// CPP implementation to find Remainder // when a large Number is divided by R #include <bits/stdc++.h> using namespace std; // Function to Return Remainder int Remainder(string str, int R) { // len is variable to store the // length of Number string. int len = str.length(); int Num, Rem = 0; // loop that find Remainder for (int i = 0; i < len; i++) { Num = Rem * 10 + (str[i] - '0'); Rem = Num % R; } // Return the remainder return Rem; } // Driver code int main() { // Get the large number as string string str = "13589234356546756"; // Get the divisor R int R = 13; // Find and print the remainder cout << Remainder(str, R); return 0; } |
chevron_right
filter_none
Java
// Java implementation to find Remainder // when a large Number is divided by R class GFG { // Function to Return Remainder static int Remainder(String str, int R) { // len is variable to store the // length of Number string. int len = str.length(); int Num, Rem = 0; // loop that find Remainder for (int i = 0; i < len; i++) { Num = Rem * 10 + (str.charAt(i) - '0'); Rem = Num % R; } // Return the remainder return Rem; } // Driver code public static void main( String [] args) { // Get the large number as string String str = "13589234356546756"; // Get the divisor R int R = 13; // Find and print the remainder System.out.println(Remainder(str, R)); } } // This code is contributed // by ihritik |
chevron_right
filter_none
Python 3
# Python 3 implementation to # find Remainder when a large # Number is divided by R # Function to Return Remainder def Remainder(str, R): # len is variable to store the # length of Number string. l = len(str) Rem = 0 # loop that find Remainder for i in range(l): Num = Rem * 10 + (ord(str[i]) - ord('0')) Rem = Num % R # Return the remainder return Rem # Driver code if __name__ == "__main__": # Get the large number # as string str = "13589234356546756" # Get the divisor R R = 13 # Find and print the remainder print(Remainder(str, R)) # This code is contributed # by ChitraNayal |
chevron_right
filter_none
C#
// C# implementation to find // Remainder when a large // Number is divided by R using System; class GFG { // Function to Return Remainder static int Remainder(String str, int R) { // len is variable to store the // length of Number string. int len = str.Length; int Num, Rem = 0; // loop that find Remainder for (int i = 0; i < len; i++) { Num = Rem * 10 + (str[i] - '0'); Rem = Num % R; } // Return the remainder return Rem; } // Driver code public static void Main() { // Get the large number as string String str = "13589234356546756"; // Get the divisor R int R = 13; // Find and print the remainder Console.WriteLine(Remainder(str, R)); } } // This code is contributed // by Subhadeep |
chevron_right
filter_none
PHP
<?php // PHP implementation to find Remainder // when a large Number is divided by R // Function to Return Remainder function Remainder($str, $R) { // len is variable to store the // length of Number string. $len = strlen($str); $Num = 0; $Rem = 0; // loop that find Remainder for ($i = 0; $i < $len; $i++) { $Num = $Rem * 10 + ($str[$i] - '0'); $Rem = $Num % $R; } // Return the remainder return $Rem; } // Driver code // Get the large number as string $str = "13589234356546756"; // Get the divisor R $R = 13; // Find and print the remainder echo Remainder($str, $R); // This code is contributed // by Akanksha Rai(Abby_akku) |
chevron_right
filter_none
Output:
11
Recommended Posts:
- Program to find remainder when large number is divided by 11
- Find the remainder when First digit of a number is divided by its Last digit
- Find remainder of array multiplication divided by n
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Find N % 4 (Remainder with 4) for a large value of N
- Check if a large number can be divided into two or more segments of equal sum
- Remainder with 7 for large numbers
- Program to find remainder without using modulo or % operator
- Program for quotient and remainder of big number
- Find minimum number to be divided to make a number a perfect square
- Find permutation with maximum remainder Sum
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Find (a^b)%m where 'a' is very large
- Find (a^b)%m where 'b' is very large
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



