- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Sum of the numbers up to N that are divisible by 2 or 5 in c programming

Sum of n natural numbers that are divisible by 2 or 5 can be found by finding the sum of all natural numbers up to N that is divisible by 2 and sum of all natural numbers up to N that is divisible by 5. Adding these two sums and then subtracting it by the sum of natural numbers up to N that is divisible by 10, this gives us the desired result. This method is an efficient method that can be used to find sum up to large values of n.

Some of you must be thinking of using a loop and conditional statement and then adding all those numbers that are divisible by 2 or 5 but this method is inefficient because it has a time complexity of the order n. This means that for large values of n the program will run the loop n number of times. And this execution will make the program heavier.

Formula to find the sum of n natural numbers divisible by 2

Sum2 = ((n / 2) * (4 + (n / 2 - 1) * 2)) / 2

Formula to find the sum of n natural numbers divisible by 5

Sum5 = ((n / 5) * (10 + (n / 5 - 1) * 5)) / 2

Formula to find the sum of n natural numbers divisible by 10

Sum10 = ((n / 10) * (20 + (n / 10 - 1) * 10)) / 2

Desired output

Sum = Sum2 + Sum5 - Sum10

## Example

#include <stdio.h> int main() { int n = 25; long int sum2, sum5, sum10; sum2 = ((n / 2) * (4 + (n / 2 - 1) * 2)) / 2; sum5 = ((n / 5) * (10 + (n / 5 - 1) * 5)) / 2; sum10 = ((n / 10) * (20 + (n / 10 - 1) * 10)) / 2; long int sum = sum2 + sum5 - sum10; printf("Sum is %d", sum); return 0; }

## Output

Sum is 201

- Related Questions & Answers
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Sum of first N natural numbers which are divisible by X or Y
- Largest number with the given set of N digits that is divisible by 2, 3 and 5 in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Minimize the sum of squares of sum of N/2 paired formed by N numbers in C++
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n - 1)^2 in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- C++ program to find the sum of the series (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + … + (n*n)
- Sum of series 1^2 + 3^2 + 5^2 + . . . + (2*n – 1)^2
- Program to print all the numbers divisible by 3 and 5 in C++
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Check if a large number is divisible by 2, 3 and 5 or not in C++
- Find Multiples of 2 or 3 or 5 less than or equal to N in C++
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’