Sum of n natural numbers

Here given code implementation process.

// C Program
// Sum of n natural numbers
#include <stdio.h>

//Calculate the sum of n natural numbers
void sum_natural_no(int n)
{
	if (n <= 0)
	{
		return;
	}
	// Formula 
	// n*(n+1) / 2
	long long int sum = n *(n + 1) / 2;
	//Display calculated result
	printf(" Sum of %d natural numbers is %lld\n", n, sum);
}
int main()
{
	//Test case
	sum_natural_no(4);
	sum_natural_no(10);
	return 0;
}

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
// Java Program 
// Sum of n natural numbers
class MyMaths
{
	//Calculate the sum of n natural numbers
	public void sum_natural_no(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Formula 
		// n*(n+1) / 2
		long sum = n * (n + 1) / 2;
		//Display calculated result
		System.out.print(" Sum of " + n + " natural numbers is " + sum + "\n");
	}
	public static void main(String[] args)
	{
		MyMaths obj = new MyMaths();
		//Test case
		obj.sum_natural_no(4);
		obj.sum_natural_no(10);
	}
}

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
//Include header file
#include <iostream>

using namespace std;
// C++ Program 
// Sum of n natural numbers
class MyMaths
{
	public:
		//Calculate the sum of n natural numbers
		void sum_natural_no(int n)
		{
			if (n <= 0)
			{
				return;
			}
			// Formula 
			// n*(n+1) / 2
			long sum = n * (n + 1) / 2;
			//Display calculated result
			cout << " Sum of " << n << " natural numbers is " << sum << "\n";
		}
};
int main()
{
	MyMaths obj = MyMaths();
	//Test case
	obj.sum_natural_no(4);
	obj.sum_natural_no(10);
	return 0;
}

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
//Include namespace system
using System;
// C# Program 
// Sum of n natural numbers
class MyMaths
{
	//Calculate the sum of n natural numbers
	public void sum_natural_no(int n)
	{
		if (n <= 0)
		{
			return;
		}
		// Formula 
		// n*(n+1) / 2
		long sum = n * (n + 1) / 2;
		//Display calculated result
		Console.Write(" Sum of " + n + " natural numbers is " + sum + "\n");
	}
	public static void Main(String[] args)
	{
		MyMaths obj = new MyMaths();
		//Test case
		obj.sum_natural_no(4);
		obj.sum_natural_no(10);
	}
}

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
<?php
// Php Program 
// Sum of n natural numbers
class MyMaths
{
	//Calculate the sum of n natural numbers
	public	function sum_natural_no($n)
	{
		if ($n <= 0)
		{
			return;
		}
		// Formula 
		// n*(n+1) / 2
		$sum = intval($n * ($n + 1) / 2);
		//Display calculated result
		echo " Sum of ". $n ." natural numbers is ". $sum ."\n";
	}
}

function main()
{
	$obj = new MyMaths();
	//Test case
	$obj->sum_natural_no(4);
	$obj->sum_natural_no(10);
}
main();

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
// Node Js Program 
// Sum of n natural numbers
class MyMaths
{
	//Calculate the sum of n natural numbers
	sum_natural_no(n)
	{
		if (n <= 0)
		{
			return;
		}
		// Formula 
		// n*(n+1) / 2
		var sum = parseInt(n * (n + 1) / 2);
		//Display calculated result
		process.stdout.write(" Sum of " + n + " natural numbers is " + sum + "\n");
	}
}

function main()
{
	var obj = new MyMaths();
	//Test case
	obj.sum_natural_no(4);
	obj.sum_natural_no(10);
}
main();

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
#  Python 3 Program 
#  Sum of n natural numbers
class MyMaths :
	# Calculate the sum of n natural numbers
	def sum_natural_no(self, n) :
		if (n <= 0) :
			return
		
		#  Formula 
		#  n*(n+1) / 2
		sum = int(n * (n + 1) / 2)
		# Display calculated result
		print(" Sum of ", n ," natural numbers is ", sum )
	

def main() :
	obj = MyMaths()
	# Test case
	obj.sum_natural_no(4)
	obj.sum_natural_no(10)

if __name__ == "__main__": main()

Output

 Sum of  4  natural numbers is  10
 Sum of  10  natural numbers is  55
#  Ruby Program 
#  Sum of n natural numbers
class MyMaths

	# Calculate the sum of n natural numbers
	def sum_natural_no(n)
	
		if (n <= 0)
		
			return
		end
		#  Formula 
		#  n*(n+1) / 2
		sum = n * (n + 1) / 2
		# Display calculated result
		print(" Sum of ", n ," natural numbers is ", sum ,"\n")
	end
end
def main()

	obj = MyMaths.new()
	# Test case
	obj.sum_natural_no(4)
	obj.sum_natural_no(10)
end
main()

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
// Scala Program 
// Sum of n natural numbers
class MyMaths
{
	//Calculate the sum of n natural numbers
	def sum_natural_no(n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		// Formula 
		// n*(n+1) / 2
		var sum: Long = (n * (n + 1) / 2).toInt;
		//Display calculated result
		print(" Sum of " + n + " natural numbers is " + sum + "\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyMaths = new MyMaths();
		//Test case
		obj.sum_natural_no(4);
		obj.sum_natural_no(10);
	}
}

Output

 Sum of 4 natural numbers is 10
 Sum of 10 natural numbers is 55
// Swift Program 
// Sum of n natural numbers
class MyMaths
{
	//Calculate the sum of n natural numbers
	func sum_natural_no(_ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		// Formula 
		// n*(n+1) / 2
		let sum: Int = n * (n + 1) / 2;
		//Display calculated result
		print(" Sum of ", n ," natural numbers is ", sum );
	}
}
func main()
{
	let obj: MyMaths = MyMaths();
	//Test case
	obj.sum_natural_no(4);
	obj.sum_natural_no(10);
}
main();

Output

 Sum of  4  natural numbers is  10
 Sum of  10  natural numbers is  55


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