# Sum of first N natural numbers which are divisible by 2 and 7

Given a number N. The task is to find the sum of all those numbers from 1 to N that are divisible by 2 or by 7.

**Examples**:

Input: N = 7Output: 19 sum = 2 + 4 + 6 + 7Input: N = 14Output: 63 sum = 2 + 4 + 6 + 7 + 8 + 10 + 12 + 14

**Approach**: To solve the problem, follow the below steps:

->Find the sum of numbers that are divisible by 2 upto N. Denote it by S1.

->Find the sum of numbers that are divisible by 7 upto N. Denote it by S2.

->Find the sum of numbers that are divisible by 14(2*7) upto N. Denote it by S3.

->The final answer will be S1 + S2 – S3.

In order to find the sum, we can use the general formula of A.P. which is:

Sn = (n/2) * {2*a + (n-1)*d}

For S1: The total numbers that will be divisible by 2 upto N will be N/2 and the series will be 2, 4, 6, 8, ….

Hence, S1 = ((N/2)/2) * (2 * 2 + (N/2 - 1) * 2)

For S2: The total numbers that will be divisible by 7 up to N will be N/7 and the series will be 7, 14, 21, 28, ……

Hence, S2 = ((N/7)/2) * (2 * 7 + (N/7 - 1) * 7)

For S3: The total numbers that will be divisible by 14 upto N will be N/14.

Hence, S3 = ((N/14)/2) * (2 * 14 + (N/14 - 1) * 14)

Therefore, the result will be:

S = S1 + S2 - S3

Below is the implementation of the above approach:

## C++

`// C++ program to find sum of numbers from 1 to N ` `// which are divisible by 2 or 7 ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the sum ` `// of numbers divisible by 2 or 7 ` `int` `sum(` `int` `N) ` `{ ` ` ` `int` `S1, S2, S3; ` ` ` ` ` `S1 = ((N / 2)) * (2 * 2 + (N / 2 - 1) * 2) / 2; ` ` ` `S2 = ((N / 7)) * (2 * 7 + (N / 7 - 1) * 7) / 2; ` ` ` `S3 = ((N / 14)) * (2 * 14 + (N / 14 - 1) * 14) / 2; ` ` ` ` ` `return` `S1 + S2 - S3; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 20; ` ` ` ` ` `cout << sum(N); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of ` `// numbers from 1 to N which ` `// are divisible by 2 or 7 ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` `// Function to calculate the sum ` `// of numbers divisible by 2 or 7 ` `public` `static` `int` `sum(` `int` `N) ` `{ ` ` ` `int` `S1, S2, S3; ` ` ` ` ` `S1 = ((N / ` `2` `)) * (` `2` `* ` `2` `+ ` ` ` `(N / ` `2` `- ` `1` `) * ` `2` `) / ` `2` `; ` ` ` `S2 = ((N / ` `7` `)) * (` `2` `* ` `7` `+ ` ` ` `(N / ` `7` `- ` `1` `) * ` `7` `) / ` `2` `; ` ` ` `S3 = ((N / ` `14` `)) * (` `2` `* ` `14` `+ ` ` ` `(N / ` `14` `- ` `1` `) * ` `14` `) / ` `2` `; ` ` ` ` ` `return` `S1 + S2 - S3; ` `} ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) { ` ` ` ` ` `int` `N = ` `20` `; ` ` ` `System.out.println( sum(N)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of ` `# above approach ` ` ` `# Function to calculate the sum ` `# of numbers divisible by 2 or 7 ` `def` `sum` `(N): ` ` ` ` ` `S1 ` `=` `((N ` `/` `/` `2` `)) ` `*` `(` `2` `*` `2` `+` `(N ` `/` `/` `2` `-` `1` `) ` `*` `2` `) ` `/` `/` `2` ` ` `S2 ` `=` `((N ` `/` `/` `7` `)) ` `*` `(` `2` `*` `7` `+` `(N ` `/` `/` `7` `-` `1` `) ` `*` `7` `) ` `/` `/` `2` ` ` `S3 ` `=` `((N ` `/` `/` `14` `)) ` `*` `(` `2` `*` `14` `+` `(N ` `/` `/` `14` `-` `1` `) ` `*` `14` `) ` `/` `/` `2` ` ` ` ` `return` `S1 ` `+` `S2 ` `-` `S3 ` ` ` ` ` `# Driver code ` `if` `__name__` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `20` ` ` ` ` `print` `(` `sum` `(N)) ` ` ` `# This code is written by ` `# Sanjit_Prasad ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of ` `// numbers from 1 to N which ` `// are divisible by 2 or 7 ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to calculate the sum ` `// of numbers divisible by 2 or 7 ` `public` `static` `int` `sum(` `int` `N) ` `{ ` ` ` `int` `S1, S2, S3; ` ` ` ` ` `S1 = ((N / 2)) * (2 * 2 + ` ` ` `(N / 2 - 1) * 2) / 2; ` ` ` `S2 = ((N / 7)) * (2 * 7 + ` ` ` `(N / 7 - 1) * 7) / 2; ` ` ` `S3 = ((N / 14)) * (2 * 14 + ` ` ` `(N / 14 - 1) * 14) / 2; ` ` ` ` ` `return` `S1 + S2 - S3; ` `} ` ` ` `// Driver code ` `public` `static` `int` `Main() ` `{ ` ` ` `int` `N = 20; ` ` ` `Console.WriteLine( sum(N)); ` ` ` `return` `0; ` `} ` `} ` ` ` `// This code is contributed ` `// by SoumikMondal ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of numbers ` `// from 1 to N which are divisible by 2 or 7 ` ` ` `// Function to calculate the sum ` `// of numbers divisible by 2 or 7 ` `function` `sum(` `$N` `) ` `{ ` ` ` `$S1` `= (int)((` `$N` `/ 2)) * (int)(2 * 2 + ` ` ` `(int)(` `$N` `/ 2 - 1) * 2) / 2; ` ` ` `$S2` `= (int)((` `$N` `/ 7)) * (int)(2 * 7 + ` ` ` `(int)(` `$N` `/ 7 - 1) * 7) / 2; ` ` ` `$S3` `= (int)((` `$N` `/ 14)) * (int)(2 * 14 + ` ` ` `(int)(` `$N` `/ 14 - 1) * 14) / 2; ` ` ` ` ` `return` `(` `$S1` `+ ` `$S2` `) - ` `$S3` `; ` `} ` ` ` `// Driver code ` `$N` `= 20; ` ` ` `echo` `sum(` `$N` `); ` ` ` `// This Code is Contributed by akt_mit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

117

## Recommended Posts:

- Sum of first N natural numbers which are divisible by X or Y
- Check whether factorial of N is divisible by sum of first N natural numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count natural numbers whose factorials are divisible by x but not y
- Check if product of first N natural numbers is divisible by their sum
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Find the first natural number whose factorial is divisible by x
- Sum of sum of first n natural numbers
- Natural Numbers
- LCM of First n Natural Numbers
- Repeated sum of first N natural numbers
- Sum of all odd natural numbers in range L and R
- Sum of cubes of even and odd natural numbers
- Average of first n even natural numbers
- Sum of fifth powers of the first n natural numbers

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.