# Sum of all the multiples of 3 and 5 in n natural numbers

Here given code implementation process.

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

void sumMultipleOf3and5(int n)
{
int result = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15) elements in given n
int three = n / 3;
int five = n / 5;
int fifteen = n / 15;
// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = ((three) *(1 + three) *3) / 2;
}
if (five > 0)
{
// Sum of multiples in 5
result += (((five) *(1 + five) *5) / 2);
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (((fifteen) *(1 + fifteen) *15) / 2);
}
}
printf("\n Given n : %d", n);
// Display calculated result
printf("\n Result  : %d", result);
}
int main(int argc, char const *argv[])
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
sumMultipleOf3and5(10);
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
sumMultipleOf3and5(21);
return 0;
}``````

#### Output

`````` Given n : 10
Result  : 33
Given n : 21
Result  : 119``````
``````/*
Java Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
public class Multiple
{
public void sumMultipleOf3and5(int n)
{
int result = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
int three = n / 3;
int five = n / 5;
int fifteen = n / 15;
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = ((three) * (1 + three) * 3) / 2;
}
if (five > 0)
{
// Sum of multiples in 5
result += (((five) * (1 + five) * 5) / 2);
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (((fifteen) * (1 + fifteen) * 15) / 2);
}
}
System.out.print("\n Given n : " + n);
// Display calculated result
System.out.print("\n Result : " + result);
}
public static void main(String[] args)
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}
}``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
class Multiple
{
public: void sumMultipleOf3and5(int n)
{
int result = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
int three = n / 3;
int five = n / 5;
int fifteen = n / 15;
// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = ((three) *(1 + three) *3) / 2;
}
if (five > 0)
{
// Sum of multiples in 5
result += (((five) *(1 + five) *5) / 2);
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (((fifteen) *(1 + fifteen) *15) / 2);
}
}
cout << "\n Given n : " << n;
// Display calculated result
cout << "\n Result : " << result;
}
};
int main()
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
return 0;
}``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````// Include namespace system
using System;
/*
Csharp Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
public class Multiple
{
public void sumMultipleOf3and5(int n)
{
int result = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
int three = n / 3;
int five = n / 5;
int fifteen = n / 15;
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = ((three) * (1 + three) * 3) / 2;
}
if (five > 0)
{
// Sum of multiples in 5
result += (((five) * (1 + five) * 5) / 2);
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (((fifteen) * (1 + fifteen) * 15) / 2);
}
}
Console.Write("\n Given n : " + n);
// Display calculated result
Console.Write("\n Result : " + result);
}
public static void Main(String[] args)
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}
}``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````<?php
/*
Php Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
class Multiple
{
public	function sumMultipleOf3and5(\$n)
{
\$result = 0;
if (\$n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
\$three = (int)(\$n / 3);
\$five = (int)(\$n / 5);
\$fifteen = (int)(\$n / 15);
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (\$three > 0)
{
// Sum of multiples in 3
\$result = (int)(((\$three) * (1 + \$three) * 3) / 2);
}
if (\$five > 0)
{
// Sum of multiples in 5
\$result += ((int)(((\$five) * (1 + \$five) * 5) / 2));
}
if (\$fifteen > 0)
{
// Remove all similar multiples of 3 and 5
\$result -= ((int)(((\$fifteen) * (1 + \$fifteen) * 15) / 2));
}
}
echo("\n Given n : ".\$n);
// Display calculated result
echo("\n Result : ".\$result);
}
}

function main()
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}
main();``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````/*
Node JS Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
class Multiple
{
sumMultipleOf3and5(n)
{
var result = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
var three = parseInt(n / 3);
var five = parseInt(n / 5);
var fifteen = parseInt(n / 15);
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = parseInt(((three) * (1 + three) * 3) / 2);
}
if (five > 0)
{
// Sum of multiples in 5
result += (parseInt(((five) * (1 + five) * 5) / 2));
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (parseInt(((fifteen) * (1 + fifteen) * 15) / 2));
}
}
process.stdout.write("\n Given n : " + n);
// Display calculated result
process.stdout.write("\n Result : " + result);
}
}

function main()
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}
main();``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````#    Python 3 Program for
#    Sum of all the multiples of 3 and 5 in n natural numbers
class Multiple :
def sumMultipleOf3and5(self, n) :
result = 0
if (n > 0) :
#  Count the multiples occurrence of (3,5,and 15)
#  elements in given n
three = int(n / 3)
five = int(n / 5)
fifteen = int(n / 15)
#  Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0) :
#  Sum of multiples in 3
result = int(((three) * (1 + three) * 3) / 2)

if (five > 0) :
#  Sum of multiples in 5
result += (int(((five) * (1 + five) * 5) / 2))

if (fifteen > 0) :
#  Remove all similar multiples of 3 and 5
result -= (int(((fifteen) * (1 + fifteen) * 15) / 2))

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

def main() :
#  Test A
#  n = 10
#  Range : [1,2,3,4,5,6,7,8,9,10]
#  Multiples [3+5+6+9+10]
#  Result = 33
#  Test B
#  n = 21
#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
#  Multiples : [3+5+6+9+10+12+15+18+20+21]
#  Result    = 119

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

#### Output

`````` Given n :  10
Result :  33
Given n :  21
Result :  119``````
``````#    Ruby Program for
#    Sum of all the multiples of 3 and 5 in n natural numbers
class Multiple
def sumMultipleOf3and5(n)
result = 0
if (n > 0)
#  Count the multiples occurrence of (3,5,and 15)
#  elements in given n
three = n / 3
five = n / 5
fifteen = n / 15
#  Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0)
#  Sum of multiples in 3
result = ((three) * (1 + three) * 3) / 2
end

if (five > 0)
#  Sum of multiples in 5
result += (((five) * (1 + five) * 5) / 2)
end

if (fifteen > 0)
#  Remove all similar multiples of 3 and 5
result -= (((fifteen) * (1 + fifteen) * 15) / 2)
end

end

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

end

def main()
#  Test A
#  n = 10
#  Range : [1,2,3,4,5,6,7,8,9,10]
#  Multiples [3+5+6+9+10]
#  Result = 33
#  Test B
#  n = 21
#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
#  Multiples : [3+5+6+9+10+12+15+18+20+21]
#  Result    = 119
end

main()``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````/*
Scala Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
class Multiple()
{
def sumMultipleOf3and5(n: Int): Unit = {
var result: Int = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
var three: Int = n / 3;
var five: Int = n / 5;
var fifteen: Int = n / 15;
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = ((three) * (1 + three) * 3) / 2;
}
if (five > 0)
{
// Sum of multiples in 5
result += (((five) * (1 + five) * 5) / 2);
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (((fifteen) * (1 + fifteen) * 15) / 2);
}
}
print("\n Given n : " + n);
// Display calculated result
print("\n Result : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Multiple = new Multiple();
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}
}``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````/*
Swift 4 Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
class Multiple
{
func sumMultipleOf3and5(_ n: Int)
{
var result: Int = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
let three: Int = n / 3;
let five: Int = n / 5;
let fifteen: Int = n / 15;
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = ((three) * (1 + three) * 3) / 2;
}
if (five > 0)
{
// Sum of multiples in 5
result += (((five) * (1 + five) * 5) / 2);
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (((fifteen) * (1 + fifteen) * 15) / 2);
}
}
print("\n Given n : ", n, terminator: "");
// Display calculated result
print("\n Result : ", result, terminator: "");
}
}
func main()
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}
main();``````

#### Output

`````` Given n :  10
Result :  33
Given n :  21
Result :  119``````
``````/*
Kotlin Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
class Multiple
{
fun sumMultipleOf3and5(n: Int): Unit
{
var result: Int = 0;
if (n > 0)
{
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
val three: Int = n / 3;
val five: Int = n / 5;
val fifteen: Int = n / 15;
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if (three > 0)
{
// Sum of multiples in 3
result = ((three) * (1 + three) * 3) / 2;
}
if (five > 0)
{
// Sum of multiples in 5
result += (((five) * (1 + five) * 5) / 2);
}
if (fifteen > 0)
{
// Remove all similar multiples of 3 and 5
result -= (((fifteen) * (1 + fifteen) * 15) / 2);
}
}
print("\n Given n : " + n);
// Display calculated result
print("\n Result : " + result);
}
}
fun main(args: Array < String > ): Unit
{
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119``````
``````package main
import "fmt"
/*
Go Program for
Sum of all the multiples of 3 and 5 in n natural numbers
*/
type Multiple struct {}
func getMultiple() * Multiple {
var me *Multiple = &Multiple {}
return me
}
func(this Multiple) sumMultipleOf3and5(n int) {
var result int = 0
if n > 0 {
// Count the multiples occurrence of (3,5,and 15)
// elements in given n
var three int = n / 3
var five int = n / 5
var fifteen int = n / 15
// Formula : (occurrence * ( occurrence + 1) * multiply ) / 2
if three > 0 {
// Sum of multiples in 3
result = ((three) * (1 + three) * 3) / 2
}
if five > 0 {
// Sum of multiples in 5
result += (((five) * (1 + five) * 5) / 2)
}
if fifteen > 0 {
// Remove all similar multiples of 3 and 5
result -= (((fifteen) * (1 + fifteen) * 15) / 2)
}
}
fmt.Print("\n Given n : ", n)
// Display calculated result
fmt.Print("\n Result : ", result)
}
func main() {
var task * Multiple = getMultiple()
// Test A
// n = 10
// Range : [1,2,3,4,5,6,7,8,9,10]
// Multiples [3+5+6+9+10]
// Result = 33
// Test B
// n = 21
// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
// Multiples : [3+5+6+9+10+12+15+18+20+21]
// Result    = 119
}``````

#### Output

`````` Given n : 10
Result : 33
Given n : 21
Result : 119`````` ## 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.