Remainder with 7 for large numbers | C ++ program to find remainder of a large number when divided by 7. ; Function which returns Remainder after dividing the number by 7 ; This series is used to find remainder with 7 ; Index of next element in series ; Traverse num from end ; Find current digit of nun ; Add next term to result ; Move to next term in series ; Make sure that result never goes beyond 7. ; Make sure that remainder is positive ; Driver program
#include <iostream> NEW_LINE using namespace std ; int remainderWith7 ( string num ) { int series [ ] = { 1 , 3 , 2 , -1 , -3 , -2 } ; int series_index = 0 ; for ( int i = num . size ( ) - 1 ; i >= 0 ; i -- ) { int digit = num [ i ] - '0' ; result += digit * series [ series_index ] ; series_index = ( series_index + 1 ) % 6 ; result %= 7 ; } if ( result < 0 ) result = ( result + 7 ) % 7 ; return result ; } int main ( ) { string str = "12345" ; cout << " Remainder ▁ with ▁ 7 ▁ is ▁ " << remainderWith7 ( str ) ; return 0 ; }