Sum of natural numbers in a range

Here given code implementation process.

// C Program
// Sum of natural numbers in given range
#include <stdio.h>

//Calculating the sum of n natural numbers from two numbers
void sum_natural_number(int first, int last)
{
	if (first <= 0 || last <= 0)
	{
		return;
	}
	else if (first > last)
	{
		//Make a valid range
		sum_natural_number(last, first);
	}
	else
	{
		//First count  number elements
		int element = (last - first);
		//Formula
		// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
		long long int sum = first * (element + 1) + ((element) * (element + 1) / 2);
		//Display calculated result
		printf("Sum of natural numbers from ( %d to %d ) is %lld\n", first, last, sum);
	}
}
int main()
{
	//Test case
	sum_natural_number(5, 10);
	sum_natural_number(20, 25);
	sum_natural_number(4, 14);
	sum_natural_number(5, 13);
	return 0;
}

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
// Java Program 
// Sum of natural numbers in given range
class MyMaths
{
	//Calculating the sum of n natural numbers from two numbers
	public void sum_natural_number(int first, int last)
	{
		if (first <= 0 || last <= 0)
		{
			return;
		}
		else if (first > last)
		{
			//Make a valid range
			sum_natural_number(last, first);
		}
		else
		{
			//First count  number elements
			int element = (last - first);
			//Formula
			// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			long sum = first * (element + 1) + ((element) * (element + 1) / 2);
			//Display calculated result
			System.out.print("Sum of natural numbers from ( " + first + " to " + last + " ) is " + sum + "\n");
		}
	}
	public static void main(String[] args)
	{
		MyMaths obj = new MyMaths();
		//Test case
		obj.sum_natural_number(5, 10);
		obj.sum_natural_number(20, 25);
		obj.sum_natural_number(4, 14);
		obj.sum_natural_number(5, 13);
	}
}

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
//Include header file
#include <iostream>

using namespace std;
// C++ Program 
// Sum of natural numbers in given range
class MyMaths
{
	public:
		//Calculating the sum of n natural numbers from two numbers
		void sum_natural_number(int first, int last)
		{
			if (first <= 0 || last <= 0)
			{
				return;
			}
			else if (first > last)
			{
				//Make a valid range
				this->sum_natural_number(last, first);
			}
			else
			{
				//First count  number elements
				int element = (last - first);
				//Formula
				// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
				long sum = first * (element + 1) + ((element) * (element + 1) / 2);
				//Display calculated result
				cout << "Sum of natural numbers from ( " << first << " to " << last << " ) is " << sum << "\n";
			}
		}
};
int main()
{
	MyMaths obj = MyMaths();
	//Test case
	obj.sum_natural_number(5, 10);
	obj.sum_natural_number(20, 25);
	obj.sum_natural_number(4, 14);
	obj.sum_natural_number(5, 13);
	return 0;
}

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
//Include namespace system
using System;
// C# Program 
// Sum of natural numbers in given range
class MyMaths
{
	//Calculating the sum of n natural numbers from two numbers
	public void sum_natural_number(int first, int last)
	{
		if (first <= 0 || last <= 0)
		{
			return;
		}
		else if (first > last)
		{
			//Make a valid range
			sum_natural_number(last, first);
		}
		else
		{
			//First count  number elements
			int element = (last - first);
			//Formula
			// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			long sum = first * (element + 1) + ((element) * (element + 1) / 2);
			//Display calculated result
			Console.Write("Sum of natural numbers from ( " + first + " to " + last + " ) is " + sum + "\n");
		}
	}
	public static void Main(String[] args)
	{
		MyMaths obj = new MyMaths();
		//Test case
		obj.sum_natural_number(5, 10);
		obj.sum_natural_number(20, 25);
		obj.sum_natural_number(4, 14);
		obj.sum_natural_number(5, 13);
	}
}

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
<?php
// Php Program 
// Sum of natural numbers in given range
class MyMaths
{
	//Calculating the sum of n natural numbers from two numbers
	public	function sum_natural_number($first, $last)
	{
		if ($first <= 0 || $last <= 0)
		{
			return;
		}
		else if ($first > $last)
		{
			//Make a valid range
			$this->sum_natural_number($last, $first);
		}
		else
		{
			//First count  number elements
			$element = ($last - $first);
			//Formula
			// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			$sum = $first * ($element + 1) + (intval(($element) * ($element + 1) / 2));
			//Display calculated result
			echo "Sum of natural numbers from ( ". $first ." to ". $last ." ) is ". $sum ."\n";
		}
	}
}

function main()
{
	$obj = new MyMaths();
	//Test case
	$obj->sum_natural_number(5, 10);
	$obj->sum_natural_number(20, 25);
	$obj->sum_natural_number(4, 14);
	$obj->sum_natural_number(5, 13);
}
main();

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
// Node Js Program 
// Sum of natural numbers in given range
class MyMaths
{
	//Calculating the sum of n natural numbers from two numbers
	sum_natural_number(first, last)
	{
		if (first <= 0 || last <= 0)
		{
			return;
		}
		else if (first > last)
		{
			//Make a valid range
			this.sum_natural_number(last, first);
		}
		else
		{
			//First count  number elements
			var element = (last - first);
			//Formula
			// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			var sum = first * (element + 1) + (parseInt((element) * (element + 1) / 2));
			//Display calculated result
			process.stdout.write("Sum of natural numbers from ( " + first + " to " + last + " ) is " + sum + "\n");
		}
	}
}

function main()
{
	var obj = new MyMaths();
	//Test case
	obj.sum_natural_number(5, 10);
	obj.sum_natural_number(20, 25);
	obj.sum_natural_number(4, 14);
	obj.sum_natural_number(5, 13);
}
main();

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
#  Python 3 Program 
#  Sum of natural numbers in given range
class MyMaths :
	# Calculating the sum of n natural numbers from two numbers
	def sum_natural_number(self, first, last) :
		if (first <= 0 or last <= 0) :
			return
		
		elif(first > last) :
			# Make a valid range
			self.sum_natural_number(last, first)
		else :
			# First count  number elements
			element = (last - first)
			# Formula
			#  first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			sum = first * (element + 1) + (int((element) * (element + 1) / 2))
			# Display calculated result
			print("Sum of natural numbers from ( ", first ," to ", last ," ) is ", sum )
		
	

def main() :
	obj = MyMaths()
	# Test case
	obj.sum_natural_number(5, 10)
	obj.sum_natural_number(20, 25)
	obj.sum_natural_number(4, 14)
	obj.sum_natural_number(5, 13)

if __name__ == "__main__": main()

Output

Sum of natural numbers from (  5  to  10  ) is  45
Sum of natural numbers from (  20  to  25  ) is  135
Sum of natural numbers from (  4  to  14  ) is  99
Sum of natural numbers from (  5  to  13  ) is  81
#  Ruby Program 
#  Sum of natural numbers in given range
class MyMaths

	# Calculating the sum of n natural numbers from two numbers
	def sum_natural_number(first, last)
	
		if (first <= 0 || last <= 0)
			return
		elsif(first > last)
			# Make a valid range
			self.sum_natural_number(last, first)
		else
		
			# First count  number elements
			element = (last - first)
			# Formula
			#  first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			sum = first * (element + 1) + ((element) * (element + 1) / 2)
			# Display calculated result
			print("Sum of natural numbers from ( ", first ," to ", last ," ) is ", sum ,"\n")
		end
	end
end
def main()

	obj = MyMaths.new()
	# Test case
	obj.sum_natural_number(5, 10)
	obj.sum_natural_number(20, 25)
	obj.sum_natural_number(4, 14)
	obj.sum_natural_number(5, 13)
end
main()

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
// Scala Program 
// Sum of natural numbers in given range
class MyMaths
{
	//Calculating the sum of n natural numbers from two numbers
	def sum_natural_number(first: Int, last: Int): Unit = {
		if (first <= 0 || last <= 0)
		{
			return;
		}
		else if (first > last)
		{
			//Make a valid range
			sum_natural_number(last, first);
		}
		else
		{
			//First count  number elements
			var element: Int = (last - first);
			//Formula
			// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			var sum: Long = first * (element + 1) + (((element) * (element + 1) / 2).toInt);
			//Display calculated result
			print("Sum of natural numbers from ( " + first + " to " + last + " ) is " + sum + "\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMaths = new MyMaths();
		//Test case
		obj.sum_natural_number(5, 10);
		obj.sum_natural_number(20, 25);
		obj.sum_natural_number(4, 14);
		obj.sum_natural_number(5, 13);
	}
}

Output

Sum of natural numbers from ( 5 to 10 ) is 45
Sum of natural numbers from ( 20 to 25 ) is 135
Sum of natural numbers from ( 4 to 14 ) is 99
Sum of natural numbers from ( 5 to 13 ) is 81
// Swift Program 
// Sum of natural numbers in given range
class MyMaths
{
	//Calculating the sum of n natural numbers from two numbers
	func sum_natural_number(_ first: Int, _ last: Int)
	{
		if (first <= 0 || last <= 0)
		{
			return;
		}
		else if (first > last)
		{
			//Make a valid range
			self.sum_natural_number(last, first);
		}
		else
		{
			//First count  number elements
			let element: Int = (last - first);
			//Formula
			// first *(total_element+1) * ((total_element) * (total_element + 1) / 2)
			let sum: Int = first * (element + 1) + ((element) * (element + 1) / 2);
			//Display calculated result
			print("Sum of natural numbers from ( ", first ," to ", last ," ) is ", sum );
		}
	}
}
func main()
{
	let obj: MyMaths = MyMaths();
	//Test case
	obj.sum_natural_number(5, 10);
	obj.sum_natural_number(20, 25);
	obj.sum_natural_number(4, 14);
	obj.sum_natural_number(5, 13);
}
main();

Output

Sum of natural numbers from (  5  to  10  ) is  45
Sum of natural numbers from (  20  to  25  ) is  135
Sum of natural numbers from (  4  to  14  ) is  99
Sum of natural numbers from (  5  to  13  ) is  81


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