Sum of the natural numbers ( up to N ) whose modulo with K yield R | C ++ implementation of the approach ; Function to return the sum ; If current number gives R as the remainder on dividing by K ; Update the sum ; Return the sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int count ( int N , int K , int R ) { long long int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % K == R ) sum += i ; } return sum ; } int main ( ) { int N = 20 , K = 4 , R = 3 ; cout << count ( N , K , R ) ; return 0 ; }