Sum of the natural numbers ( up to N ) whose modulo with K yield R | Java 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
class GfG { static long count ( int N , int K , int R ) { long sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % K == R ) sum += i ; } return sum ; } public static void main ( String [ ] args ) { int N = 20 , K = 4 , R = 3 ; System . out . println ( count ( N , K , R ) ) ; } }