Sum of all the multiples of 3 and 7 in n natural numbers

Here given code implementation process.

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

void sumMultipleOf3and7(int n)
{
	int result = 0;
	if (n > 0)
	{
		// Count the multiples occurrence of (3,7,and 21) 
    	// Elements in given n
		int three = n / 3;
		int seven = n / 7;
		int twentyOne = n / 21;
		// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
		if (three > 0)
		{
			// Sum of multiples in 3
			result = ((three) *(1 + three) *3) / 2;
		}
		if (seven > 0)
		{
			// Sum of multiples in 7
			result += (((seven) *(1 + seven) *7) / 2);
		}
		if (twentyOne > 0)
		{
			// Remove all similar multiples of 3 and 7
			result -= (((twentyOne) *(1 + twentyOne) *21) / 2);
		}
	}
	printf("\n Given n : %d", n);
	printf("\n Result  : %d", result);
}
int main(int argc, char
	const *argv[])
{
	// Test A
	// n = 20
	// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	// Multiples [3+6+7+9+12+14+15+18]
	// Result = 84
	sumMultipleOf3and7(20);
	// Test B
	// n = 30
	// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	// Result    = 214
	sumMultipleOf3and7(30);
	return 0;
}

Output

 Given n : 20
 Result  : 84
 Given n : 30
 Result  : 214
/*
    Java Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
public class Multiple
{
	public void sumMultipleOf3and7(int n)
	{
		int result = 0;
		if (n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			int three = n / 3;
			int seven = n / 7;
			int twentyOne = n / 21;
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0)
			{
				// Sum of multiples in 3
				result = ((three) * (1 + three) * 3) / 2;
			}
			if (seven > 0)
			{
				// Sum of multiples in 7
				result += (((seven) * (1 + seven) * 7) / 2);
			}
			if (twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				result -= (((twentyOne) * (1 + twentyOne) * 21) / 2);
			}
		}
		System.out.print("\n Given n : " + n);
		System.out.print("\n Result : " + result);
	}
	public static void main(String[] args)
	{
		Multiple task = new Multiple();
		// Test A
		// n = 20
		// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
		// Multiples [3+6+7+9+12+14+15+18]
		// Result = 84
		task.sumMultipleOf3and7(20);
		// Test B
		// n = 30
		// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
		// Result    = 214
		task.sumMultipleOf3and7(30);
	}
}

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
class Multiple
{
	public: void sumMultipleOf3and7(int n)
	{
		int result = 0;
		if (n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			int three = n / 3;
			int seven = n / 7;
			int twentyOne = n / 21;
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0)
			{
				// Sum of multiples in 3
				result = ((three) *(1 + three) *3) / 2;
			}
			if (seven > 0)
			{
				// Sum of multiples in 7
				result += (((seven) *(1 + seven) *7) / 2);
			}
			if (twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				result -= (((twentyOne) *(1 + twentyOne) *21) / 2);
			}
		}
		cout << "\n Given n : " << n;
		cout << "\n Result : " << result;
	}
};
int main()
{
	Multiple *task = new Multiple();
	// Test A
	// n = 20
	// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	// Multiples [3+6+7+9+12+14+15+18]
	// Result = 84
	task->sumMultipleOf3and7(20);
	// Test B
	// n = 30
	// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	// Result    = 214
	task->sumMultipleOf3and7(30);
	return 0;
}

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
// Include namespace system
using System;
/*
    Csharp Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
public class Multiple
{
	public void sumMultipleOf3and7(int n)
	{
		int result = 0;
		if (n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			int three = n / 3;
			int seven = n / 7;
			int twentyOne = n / 21;
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0)
			{
				// Sum of multiples in 3
				result = ((three) * (1 + three) * 3) / 2;
			}
			if (seven > 0)
			{
				// Sum of multiples in 7
				result += (((seven) * (1 + seven) * 7) / 2);
			}
			if (twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				result -= (((twentyOne) * (1 + twentyOne) * 21) / 2);
			}
		}
		Console.Write("\n Given n : " + n);
		Console.Write("\n Result : " + result);
	}
	public static void Main(String[] args)
	{
		Multiple task = new Multiple();
		// Test A
		// n = 20
		// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
		// Multiples [3+6+7+9+12+14+15+18]
		// Result = 84
		task.sumMultipleOf3and7(20);
		// Test B
		// n = 30
		// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
		// Result    = 214
		task.sumMultipleOf3and7(30);
	}
}

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
package main
import "fmt"
/*
    Go Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
type Multiple struct {}
func getMultiple() * Multiple {
	var me *Multiple = &Multiple {}
	return me
}
func(this Multiple) sumMultipleOf3and7(n int) {
	var result int = 0
	if n > 0 {
		// Count the multiples occurrence of (3,7,and 21) 
		// Elements in given n
		var three int = n / 3
		var seven int = n / 7
		var twentyOne int = n / 21
		// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
		if three > 0 {
			// Sum of multiples in 3
			result = ((three) * (1 + three) * 3) / 2
		}
		if seven > 0 {
			// Sum of multiples in 7
			result += (((seven) * (1 + seven) * 7) / 2)
		}
		if twentyOne > 0 {
			// Remove all similar multiples of 3 and 7
			result -= (((twentyOne) * (1 + twentyOne) * 21) / 2)
		}
	}
	fmt.Print("\n Given n : ", n)
	fmt.Print("\n Result : ", result)
}
func main() {
	var task * Multiple = getMultiple()
	// Test A
	// n = 20
	// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	// Multiples [3+6+7+9+12+14+15+18]
	// Result = 84
	task.sumMultipleOf3and7(20)
	// Test B
	// n = 30
	// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	// Result    = 214
	task.sumMultipleOf3and7(30)
}

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
<?php
/*
    Php Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
class Multiple
{
	public	function sumMultipleOf3and7($n)
	{
		$result = 0;
		if ($n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			$three = (int)($n / 3);
			$seven = (int)($n / 7);
			$twentyOne = (int)($n / 21);
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if ($three > 0)
			{
				// Sum of multiples in 3
				$result = (int)((($three) * (1 + $three) * 3) / 2);
			}
			if ($seven > 0)
			{
				// Sum of multiples in 7
				$result += ((int)((($seven) * (1 + $seven) * 7) / 2));
			}
			if ($twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				$result -= ((int)((($twentyOne) * (1 + $twentyOne) * 21) / 2));
			}
		}
		echo("\n Given n : ".$n);
		echo("\n Result : ".$result);
	}
}

function main()
{
	$task = new Multiple();
	// Test A
	// n = 20
	// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	// Multiples [3+6+7+9+12+14+15+18]
	// Result = 84
	$task->sumMultipleOf3and7(20);
	// Test B
	// n = 30
	// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	// Result    = 214
	$task->sumMultipleOf3and7(30);
}
main();

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
/*
    Node JS Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
class Multiple
{
	sumMultipleOf3and7(n)
	{
		var result = 0;
		if (n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			var three = parseInt(n / 3);
			var seven = parseInt(n / 7);
			var twentyOne = parseInt(n / 21);
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0)
			{
				// Sum of multiples in 3
				result = parseInt(((three) * (1 + three) * 3) / 2);
			}
			if (seven > 0)
			{
				// Sum of multiples in 7
				result += (parseInt(((seven) * (1 + seven) * 7) / 2));
			}
			if (twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				result -= (parseInt(((twentyOne) * (1 + twentyOne) * 21) / 2));
			}
		}
		process.stdout.write("\n Given n : " + n);
		process.stdout.write("\n Result : " + result);
	}
}

function main()
{
	var task = new Multiple();
	// Test A
	// n = 20
	// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	// Multiples [3+6+7+9+12+14+15+18]
	// Result = 84
	task.sumMultipleOf3and7(20);
	// Test B
	// n = 30
	// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	// Result    = 214
	task.sumMultipleOf3and7(30);
}
main();

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
#    Python 3 Program for
# 	Sum of all the multiples of 3 and 7 in n natural numbers
class Multiple :
	def sumMultipleOf3and7(self, n) :
		result = 0
		if (n > 0) :
			#  Count the multiples occurrence of (3,7,and 21) 
			#  Elements in given n
			three = int(n / 3)
			seven = int(n / 7)
			twentyOne = int(n / 21)
			#  Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0) :
				#  Sum of multiples in 3
				result = int(((three) * (1 + three) * 3) / 2)
			
			if (seven > 0) :
				#  Sum of multiples in 7
				result += (int(((seven) * (1 + seven) * 7) / 2))
			
			if (twentyOne > 0) :
				#  Remove all similar multiples of 3 and 7
				result -= (int(((twentyOne) * (1 + twentyOne) * 21) / 2))
			
		
		print("\n Given n : ", n, end = "")
		print("\n Result : ", result, end = "")
	

def main() :
	task = Multiple()
	#  Test A
	#  n = 20
	#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	#  Multiples [3+6+7+9+12+14+15+18]
	#  Result = 84
	task.sumMultipleOf3and7(20)
	#  Test B
	#  n = 30
	#  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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	#  Result    = 214
	task.sumMultipleOf3and7(30)

if __name__ == "__main__": main()

Output

 Given n :  20
 Result :  84
 Given n :  30
 Result :  214
#    Ruby Program for
# 	Sum of all the multiples of 3 and 7 in n natural numbers
class Multiple 
	def sumMultipleOf3and7(n) 
		result = 0
		if (n > 0) 
			#  Count the multiples occurrence of (3,7,and 21) 
			#  Elements in given n
			three = n / 3
			seven = n / 7
			twentyOne = n / 21
			#  Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0) 
				#  Sum of multiples in 3
				result = ((three) * (1 + three) * 3) / 2
			end

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

			if (twentyOne > 0) 
				#  Remove all similar multiples of 3 and 7
				result -= (((twentyOne) * (1 + twentyOne) * 21) / 2)
			end

		end

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

end

def main() 
	task = Multiple.new()
	#  Test A
	#  n = 20
	#  Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	#  Multiples [3+6+7+9+12+14+15+18]
	#  Result = 84
	task.sumMultipleOf3and7(20)
	#  Test B
	#  n = 30
	#  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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	#  Result    = 214
	task.sumMultipleOf3and7(30)
end

main()

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
/*
    Scala Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
class Multiple()
{
	def sumMultipleOf3and7(n: Int): Unit = {
		var result: Int = 0;
		if (n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			var three: Int = n / 3;
			var seven: Int = n / 7;
			var twentyOne: Int = n / 21;
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0)
			{
				// Sum of multiples in 3
				result = ((three) * (1 + three) * 3) / 2;
			}
			if (seven > 0)
			{
				// Sum of multiples in 7
				result += (((seven) * (1 + seven) * 7) / 2);
			}
			if (twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				result -= (((twentyOne) * (1 + twentyOne) * 21) / 2);
			}
		}
		print("\n Given n : " + n);
		print("\n Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Multiple = new Multiple();
		// Test A
		// n = 20
		// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
		// Multiples [3+6+7+9+12+14+15+18]
		// Result = 84
		task.sumMultipleOf3and7(20);
		// Test B
		// n = 30
		// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
		// Result    = 214
		task.sumMultipleOf3and7(30);
	}
}

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214
/*
    Swift 4 Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
class Multiple
{
	func sumMultipleOf3and7(_ n: Int)
	{
		var result: Int = 0;
		if (n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			let three: Int = n / 3;
			let seven: Int = n / 7;
			let twentyOne: Int = n / 21;
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0)
			{
				// Sum of multiples in 3
				result = ((three) * (1 + three) * 3) / 2;
			}
			if (seven > 0)
			{
				// Sum of multiples in 7
				result += (((seven) * (1 + seven) * 7) / 2);
			}
			if (twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				result -= (((twentyOne) * (1 + twentyOne) * 21) / 2);
			}
		}
		print("\n Given n : ", n, terminator: "");
		print("\n Result : ", result, terminator: "");
	}
}
func main()
{
	let task: Multiple = Multiple();
	// Test A
	// n = 20
	// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	// Multiples [3+6+7+9+12+14+15+18]
	// Result = 84
	task.sumMultipleOf3and7(20);
	// Test B
	// n = 30
	// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	// Result    = 214
	task.sumMultipleOf3and7(30);
}
main();

Output

 Given n :  20
 Result :  84
 Given n :  30
 Result :  214
/*
    Kotlin Program for
	Sum of all the multiples of 3 and 7 in n natural numbers
*/
class Multiple
{
	fun sumMultipleOf3and7(n: Int): Unit
	{
		var result: Int = 0;
		if (n > 0)
		{
			// Count the multiples occurrence of (3,7,and 21) 
			// Elements in given n
			val three: Int = n / 3;
			val seven: Int = n / 7;
			val twentyOne: Int = n / 21;
			// Formula : (occurrence *( occurrence + 1) *multiply ) / 2
			if (three > 0)
			{
				// Sum of multiples in 3
				result = ((three) * (1 + three) * 3) / 2;
			}
			if (seven > 0)
			{
				// Sum of multiples in 7
				result += (((seven) * (1 + seven) * 7) / 2);
			}
			if (twentyOne > 0)
			{
				// Remove all similar multiples of 3 and 7
				result -= (((twentyOne) * (1 + twentyOne) * 21) / 2);
			}
		}
		print("\n Given n : " + n);
		print("\n Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Multiple = Multiple();
	// Test A
	// n = 20
	// Range : [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
	// Multiples [3+6+7+9+12+14+15+18]
	// Result = 84
	task.sumMultipleOf3and7(20);
	// Test B
	// n = 30
	// 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 : [3+6+7+9+12+14+15+18+21+24+27+28+30]
	// Result    = 214
	task.sumMultipleOf3and7(30);
}

Output

 Given n : 20
 Result : 84
 Given n : 30
 Result : 214


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.

New Comment







© 2021, kalkicode.com, All rights reserved