# Sum of the multiples of two numbers in n natural numbers

Here given code implementation process.

``````// C program for
// Sum of the multiples of two numbers in n natural numbers
#include <stdio.h>

// Returns the greatest common divisor of two numbers
int gcd(int a, int b)
{
if (a == 0)
{
return b;
}
return gcd(b % a, a);
}
int multipleSum(int n, int num)
{
// Count the multiples occurrence
int element = n / num;
return ((element) *(1 + element) *num) / 2;
}
void findMultipleSum(int n, int a, int b)
{
int result = 0;
// Display given numbers
printf("\n Given n : %d", n);
printf("\n Number a : %d", a);
printf("\n Number b : %d", b);
if (n > 0)
{
result = multipleSum(n, a) + multipleSum(n, b) -
multipleSum(n, (a *b) / gcd(a, b));
}
// Display calculated result
printf("\n Result  : %d\n", result);
}
int main(int argc, char
const *argv[])
{
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
findMultipleSum(20, 3, 4);
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
findMultipleSum(30, 5, 6);
return 0;
}``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result  : 111

Given n : 30
Number a : 5
Number b : 6
Result  : 165``````
``````/*
Java Program for
Sum of the multiples of two numbers in n natural numbers
*/
public class Multiple
{
// Returns the greatest common divisor of two numbers
public int gcd(int a, int b)
{
if (a == 0)
{
return b;
}
return gcd(b % a, a);
}
public int multipleSum(int n, int num)
{
// Count the multiples occurrence
int element = n / num;
return ((element) * (1 + element) * num) / 2;
}
public void findMultipleSum(int n, int a, int b)
{
int result = 0;
// Display given numbers
System.out.print("\n Given n : " + n);
System.out.print("\n Number a : " + a);
System.out.print("\n Number b : " + b);
if (n > 0)
{
result = multipleSum(n, a) + multipleSum(n, b) -
multipleSum(n, (a * b) / gcd(a, b));
}
// Display calculated result
System.out.println("\n Result : " + result);
}
public static void main(String[] args)
{
Multiple task = new Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}
}``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Sum of the multiples of two numbers in n natural numbers
*/
class Multiple
{
public:
// Returns the greatest common divisor of two numbers
int gcd(int a, int b)
{
if (a == 0)
{
return b;
}
return this->gcd(b % a, a);
}
int multipleSum(int n, int num)
{
// Count the multiples occurrence
int element = n / num;
return ((element) * (1 + element) *num) / 2;
}
void findMultipleSum(int n, int a, int b)
{
int result = 0;
// Display given numbers
cout << "\n Given n : " << n;
cout << "\n Number a : " << a;
cout << "\n Number b : " << b;
if (n > 0)
{
result = this->multipleSum(n, a) + this->multipleSum(n, b) -
this->multipleSum(n, (a *b) / this->gcd(a, b));
}
// Display calculated result
cout << "\n Result : " << result << endl;
}
};
int main()
{
Multiple *task = new Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
return 0;
}``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````
``````// Include namespace system
using System;
/*
Csharp Program for
Sum of the multiples of two numbers in n natural numbers
*/
public class Multiple
{
// Returns the greatest common divisor of two numbers
public int gcd(int a, int b)
{
if (a == 0)
{
return b;
}
return this.gcd(b % a, a);
}
public int multipleSum(int n, int num)
{
// Count the multiples occurrence
int element = n / num;
return ((element) * (1 + element) * num) / 2;
}
public void findMultipleSum(int n, int a, int b)
{
int result = 0;
// Display given numbers
Console.Write("\n Given n : " + n);
Console.Write("\n Number a : " + a);
Console.Write("\n Number b : " + b);
if (n > 0)
{
result = this.multipleSum(n, a) + this.multipleSum(n, b) -
this.multipleSum(n, (a * b) / this.gcd(a, b));
}
// Display calculated result
Console.WriteLine("\n Result : " + result);
}
public static void Main(String[] args)
{
Multiple task = new Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}
}``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````
``````package main
import "fmt"
/*
Go Program for
Sum of the multiples of two numbers in n natural numbers
*/
type Multiple struct {}
func getMultiple() * Multiple {
var me *Multiple = &Multiple {}
return me
}
// Returns the greatest common divisor of two numbers
func(this Multiple) gcd(a, b int) int {
if a == 0 {
return b
}
return this.gcd(b % a, a)
}
func(this Multiple) multipleSum(n, num int) int {
// Count the multiples occurrence
var element int = n / num
return ((element) * (1 + element) * num) / 2
}
func(this Multiple) findMultipleSum(n, a, b int) {
var result int = 0
// Display given numbers
fmt.Print("\n Given n : ", n)
fmt.Print("\n Number a : ", a)
fmt.Print("\n Number b : ", b)
if n > 0 {
result = (this.multipleSum(n, a) + this.multipleSum(n, b)) -
this.multipleSum(n, (a * b) / this.gcd(a, b))
}
// Display calculated result
fmt.Println("\n Result : ", result)
}
func main() {
var task * Multiple = getMultiple()
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````
``````<?php
/*
Php Program for
Sum of the multiples of two numbers in n natural numbers
*/
class Multiple
{
// Returns the greatest common divisor of two numbers
public	function gcd(\$a, \$b)
{
if (\$a == 0)
{
return \$b;
}
return \$this->gcd(\$b % \$a, \$a);
}
public	function multipleSum(\$n, \$num)
{
// Count the multiples occurrence
\$element = (int)(\$n / \$num);
return (int)(((\$element) * (1 + \$element) * \$num) / 2);
}
public	function findMultipleSum(\$n, \$a, \$b)
{
\$result = 0;
// Display given numbers
echo("\n Given n : ".\$n);
echo("\n Number a : ".\$a);
echo("\n Number b : ".\$b);
if (\$n > 0)
{
\$result = \$this->multipleSum(\$n, \$a) + \$this->multipleSum(\$n, \$b) -
\$this->multipleSum(\$n, (int)((\$a * \$b) / \$this->gcd(\$a, \$b)));
}
// Display calculated result
echo("\n Result : ".\$result.
"\n");
}
}

function main()
{
\$task = new Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}
main();``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````
``````/*
Node JS Program for
Sum of the multiples of two numbers in n natural numbers
*/
class Multiple
{
// Returns the greatest common divisor of two numbers
gcd(a, b)
{
if (a == 0)
{
return b;
}
return this.gcd(b % a, a);
}
multipleSum(n, num)
{
// Count the multiples occurrence
var element = parseInt(n / num);
return parseInt(((element) * (1 + element) * num) / 2);
}
findMultipleSum(n, a, b)
{
var result = 0;
// Display given numbers
process.stdout.write("\n Given n : " + n);
process.stdout.write("\n Number a : " + a);
process.stdout.write("\n Number b : " + b);
if (n > 0)
{
result = this.multipleSum(n, a) + this.multipleSum(n, b) -
this.multipleSum(n, parseInt((a * b) / this.gcd(a, b)));
}
// Display calculated result
console.log("\n Result : " + result);
}
}

function main()
{
var task = new Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}
main();``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````
``````#    Python 3 Program for
# 	Sum of the multiples of two numbers in n natural numbers
class Multiple :
#  Returns the greatest common divisor of two numbers
def gcd(self, a, b) :
if (a == 0) :
return b

return self.gcd(b % a, a)

def multipleSum(self, n, num) :
#  Count the multiples occurrence
element = int(n / num)
return int(((element) * (1 + element) * num) / 2)

def findMultipleSum(self, n, a, b) :
result = 0
#  Display given numbers
print("\n Given n : ", n, end = "")
print("\n Number a : ", a, end = "")
print("\n Number b : ", b, end = "")
if (n > 0) :
result = self.multipleSum(n, a) + self.multipleSum(n, b) -self.multipleSum(n, int((a * b) / self.gcd(a, b)))

#  Display calculated result
print("\n Result : ", result)

def main() :
#  Test A
#  n = 20
#  a = 3
#  b = 4
#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
#  Multiples [3+4+6+8+9+12+15+16+18+20]
#  Result = 111
#  Test B
#  n = 30
#  a = 5
#  b = 6
#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
#           18,19,20,21,22,23,24,25,26,27,28,29,30]
#  Multiples : [5+6+10+12+15+18+20+24+25+30]
#  Result    = 165

if __name__ == "__main__": main()``````

#### Output

`````` Given n :  20
Number a :  3
Number b :  4
Result :  111

Given n :  30
Number a :  5
Number b :  6
Result :  165``````
``````#    Ruby Program for
# 	Sum of the multiples of two numbers in n natural numbers
class Multiple
#  Returns the greatest common divisor of two numbers
def gcd(a, b)
if (a == 0)
return b
end

return self.gcd(b % a, a)
end

def multipleSum(n, num)
#  Count the multiples occurrence
element = n / num
return ((element) * (1 + element) * num) / 2
end

def findMultipleSum(n, a, b)
result = 0
#  Display given numbers
print("\n Given n : ", n)
print("\n Number a : ", a)
print("\n Number b : ", b)
if (n > 0)
result = self.multipleSum(n, a) + self.multipleSum(n, b) -
self.multipleSum(n, (a * b) / self.gcd(a, b))
end

#  Display calculated result
print("\n Result : ", result, "\n")
end

end

def main()
#  Test A
#  n = 20
#  a = 3
#  b = 4
#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
#  Multiples [3+4+6+8+9+12+15+16+18+20]
#  Result = 111
#  Test B
#  n = 30
#  a = 5
#  b = 6
#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
#           18,19,20,21,22,23,24,25,26,27,28,29,30]
#  Multiples : [5+6+10+12+15+18+20+24+25+30]
#  Result    = 165
end

main()``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165
``````
``````/*
Scala Program for
Sum of the multiples of two numbers in n natural numbers
*/
class Multiple()
{
// Returns the greatest common divisor of two numbers
def gcd(a: Int, b: Int): Int = {
if (a == 0)
{
return b;
}
return gcd(b % a, a);
}
def multipleSum(n: Int, num: Int): Int = {
// Count the multiples occurrence
var element: Int = n / num;
return ((element) * (1 + element) * num) / 2;
}
def findMultipleSum(n: Int, a: Int, b: Int): Unit = {
var result: Int = 0;
// Display given numbers
print("\n Given n : " + n);
print("\n Number a : " + a);
print("\n Number b : " + b);
if (n > 0)
{
result = multipleSum(n, a) + multipleSum(n, b) -
multipleSum(n, (a * b) / gcd(a, b));
}
// Display calculated result
println("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiple = new Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}
}``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````
``````/*
Swift 4 Program for
Sum of the multiples of two numbers in n natural numbers
*/
class Multiple
{
// Returns the greatest common divisor of two numbers
func gcd(_ a: Int, _ b: Int) -> Int
{
if (a == 0)
{
return b;
}
return self.gcd(b % a, a);
}
func multipleSum(_ n: Int, _ num: Int) -> Int
{
// Count the multiples occurrence
let element: Int = n / num;
return ((element) * (1 + element) * num) / 2;
}
func findMultipleSum(_ n: Int, _ a: Int, _ b: Int)
{
var result: Int = 0;
// Display given numbers
print("\n Given n : ", n, terminator: "");
print("\n Number a : ", a, terminator: "");
print("\n Number b : ", b, terminator: "");
if (n > 0)
{
result = self.multipleSum(n, a) + self.multipleSum(n, b) -
self.multipleSum(n, (a * b) / self.gcd(a, b));
}
// Display calculated result
print("\n Result : ", result);
}
}
func main()
{
let task: Multiple = Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}
main();``````

#### Output

`````` Given n :  20
Number a :  3
Number b :  4
Result :  111

Given n :  30
Number a :  5
Number b :  6
Result :  165``````
``````/*
Kotlin Program for
Sum of the multiples of two numbers in n natural numbers
*/
class Multiple
{
// Returns the greatest common divisor of two numbers
fun gcd(a: Int, b: Int): Int
{
if (a == 0)
{
return b;
}
return this.gcd(b % a, a);
}
fun multipleSum(n: Int, num: Int): Int
{
// Count the multiples occurrence
val element: Int = n / num;
return ((element) * (1 + element) * num) / 2;
}
fun findMultipleSum(n: Int, a: Int, b: Int): Unit
{
var result: Int = 0;
// Display given numbers
print("\n Given n : " + n);
print("\n Number a : " + a);
print("\n Number b : " + b);
if (n > 0)
{
result = this.multipleSum(n, a) + this.multipleSum(n, b) -
this.multipleSum(n, (a * b) / this.gcd(a, b));
}
// Display calculated result
println("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: Multiple = Multiple();
// Test A
// n = 20
// a = 3
// b = 4
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
// Multiples [3+4+6+8+9+12+15+16+18+20]
// Result = 111
// Test B
// n = 30
// a = 5
// b = 6
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17 ,
//          18,19,20,21,22,23,24,25,26,27,28,29,30]
// Multiples : [5+6+10+12+15+18+20+24+25+30]
// Result    = 165
}``````

#### Output

`````` Given n : 20
Number a : 3
Number b : 4
Result : 111

Given n : 30
Number a : 5
Number b : 6
Result : 165``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.