
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Sum of first N natural numbers which are divisible by X or Y
Adding up all natural numbers up to n, that are divisible by X or Y is selecting all the numbers that are divisible by X or Y and adding them to a variable that stores the sum.
To find the sum of the first N natural numbers which are divisible by X or Y, there are two methods −
- Using loops and conditional statements
- Using formula
Method 1 − Using loops and conditional statements
This method uses a loop that counts up to n numbers and selects numbers that are divisible by X or Y and adds them and save to a variables at each iteration.
Example Code
#include <stdio.h> int main(void) { int n = 54; int x = 2 ; int y = 5; int sum = 0; for(int i = 0; i<= n; i++) { if(i%x == 0 || i% y == 0) sum = sum + i; } printf("sum of %d natural numbers divisible by %d and %d is %d" ,n,x,y,sum); return 0; }
Output
sum of 54 natural numbers divisible by 2 and 5 is 881
Method 2 − Using formula ,
This method uses the formula to find the sum of the first n number divisible by a number.
This can be found using the formula − SN/X = ((N/X)/2) * (2 * X + (N/X - 1) * X)
Using this formula the sum of n natural numbers divisible by x is found − S n/x = ((n/x)/2) * (2 * x + (n/x - 1) * x)
Using this formula the sum of n natural numbers divisible by y is found − S n/y = ((n/y)/2) * (2 * y + (n/y - 1) * y)
Now, Using this formula the sum of n natural numbers divisible by x and y is found : S n/x*y = ((n/(x*y)/2) * (2 * (x*y) + (n/(x*y) - 1) * (x*y))
Now, we will add the sum of x and sum of y and subtract sum of x*y that are add two times.
Example Code
#include <stdio.h> int main() { int n = 54; int x = 2, y = 5; int Sx, Sy, Sxy, sum; Sx = ((n / x)) * (2 * x + (n / x - 1) * x) / 2; Sy = ((n / y)) * (2 * y + (n / y - 1) * y) / 2; Sxy= ((n / (x * y))) * (2 * (x * y) + (n / (x * y) - 1) * (x * y))/ 2; sum = Sx + Sy - Sxy; printf("sum of %d natural numbers divisible by %d and %d is %d" ,n,x,y,sum); return 0; }
Output
sum of 54 natural numbers divisible by 2 and 5 is 881
The second method is better because it does not use any loop that means better time complexity. But if the input cases are smaller than first one can also be used. But for large input cases the second method is not the best option.
- Related Articles
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- Check if product of first N natural numbers is divisible by their sum in Python
- Find the sum of all 3-digit natural numbers which are divisible by 13.
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Sum of sum of first n natural numbers in C++
- Sum of square-sums of first n natural numbers
- Find the sum of all natural numbers between 1 and 100, which are divisible by 3.
- Sum of all subsets of a set formed by first n natural numbers
- Sum of first n natural numbers in C Program
- Find the sum of first $n$ odd natural numbers.
- Python Program for cube sum of first n natural numbers
- C++ Program for cube sum of first n natural numbers?
- C Program for cube sum of first n natural numbers?
