# Sum of the natural numbers (up to N) whose modulo with K yield R

Given three integers N, K and R. The task is to calculate the sum of all those numbers from 1 to N which yields remainder R upon division by K.

Examples:

Input: N = 20, K = 4, R = 3
Output: 55
3, 7, 11, 15 and 19 are the only numbers that give 3 as the remainder on division with 4.
3 + 7 + 11 + 15 + 19 = 55

Input: N = 15, K = 13, R = 2
Output: 17

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• Initialize sum = 0 and take the modulo of each element from 1 to N with K.
• If the remainder is equal to R, then update sum = sum + i where i is the current number that gave R as the remainder on dividing by K.
• Print the value of sum in the end.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the sum ` `long` `long` `int` `count(``int` `N, ``int` `K, ``int` `R) ` `{ ` `    ``long` `long` `int` `sum = 0; ` `    ``for` `(``int` `i = 1; i <= N; i++) { ` ` `  `        ``// If current number gives R as the ` `        ``// remainder on dividing by K ` `        ``if` `(i % K == R) ` ` `  `            ``// Update the sum ` `            ``sum += i; ` `    ``} ` ` `  `    ``// Return the sum ` `    ``return` `sum; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `N = 20, K = 4, R = 3; ` `    ``cout << count(N, K, R); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach  ` `class` `GfG  ` `{  ` ` `  `// Function to return the sum  ` `static` `long` `count(``int` `N, ``int` `K, ``int` `R)  ` `{  ` `    ``long` `sum = ``0``;  ` `    ``for` `(``int` `i = ``1``; i <= N; i++) ` `    ``{  ` ` `  `        ``// If current number gives R as the  ` `        ``// remainder on dividing by K  ` `        ``if` `(i % K == R)  ` ` `  `            ``// Update the sum  ` `            ``sum += i;  ` `    ``}  ` ` `  `    ``// Return the sum  ` `    ``return` `sum;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `N = ``20``, K = ``4``, R = ``3``;  ` `    ``System.out.println(count(N, K, R));  ` `} ` `}  ` ` `  `// This code is contributed by ` `// prerna saini. `

## Python3

 `# Python 3 implementation of the approach ` ` `  `# Function to return the sum ` `def` `count(N, K, R): ` `    ``sum` `=` `0` `    ``for` `i ``in` `range``(``1``, N ``+` `1``): ` `         `  `        ``# If current number gives R as the ` `        ``# remainder on dividing by K ` `        ``if` `(i ``%` `K ``=``=` `R): ` `             `  `            ``# Update the sum ` `            ``sum` `+``=` `i ` ` `  `    ``# Return the sum ` `    ``return` `sum` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `    ``N ``=` `20` `    ``K ``=` `4` `    ``R ``=` `3` `    ``print``(count(N, K, R)) ` ` `  `# This code is contributed by ` `# Surendra_Gangwar `

## C#

 `// C# implementation of the approach  ` `using` `System; ` `class` `GFG  ` `{  ` ` `  `// Function to return the sum  ` `static` `long` `count(``int` `N, ``int` `K, ``int` `R)  ` `{  ` `    ``long` `sum = 0;  ` `    ``for` `(``int` `i = 1; i <= N; i++) ` `    ``{  ` ` `  `        ``// If current number gives R as the  ` `        ``// remainder on dividing by K  ` `        ``if` `(i % K == R)  ` ` `  `            ``// Update the sum  ` `            ``sum += i;  ` `    ``}  ` ` `  `    ``// Return the sum  ` `    ``return` `sum;  ` `}  ` ` `  `// Driver code  ` `public` `static` `void` `Main()  ` `{  ` `    ``int` `N = 20, K = 4, R = 3;  ` `    ``Console.Write(count(N, K, R));  ` `} ` `}  ` ` `  `// This code is contributed by ` `// Akanksha Rai `

## PHP

 ` `

Output:

```55
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.