Pentagonal number

Here given code implementation process.

// C Program for
// Pentagonal number
#include <stdio.h>

void pentagonalNo(int k)
{
	// Print all initial k pentagonal number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		//  (3n²-n)
		//  —————————
		//     2
      
		// Calculate nth pentagonal number
		int result = (3 *(n *n) - n) / 2;
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	//  Pentagonal number are
	// —————————————————————————————————————————————
	//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
	//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
	//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
  
	// k = 10
	pentagonalNo(10);
	return 0;
}

Output

  1  5  12  22  35  51  70  92  117  145
// Java program for
// Pentagonal number
public class PentagonalNumber
{
	public void pentagonalNo(int k)
	{
		// Print all initial k pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			int result = (3 * (n * n) - n) / 2;
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		PentagonalNumber task = new PentagonalNumber();
      
		//  Pentagonal number are
		// —————————————————————————————————————————————
		//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
		//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
		//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
		//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
		//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
      
		// k = 10
		task.pentagonalNo(10);
	}
}

Output

 1 5 12 22 35 51 70 92 117 145
// Include header file
#include <iostream>
using namespace std;

// C++ program for
// Pentagonal number

class PentagonalNumber
{
	public: void pentagonalNo(int k)
	{
		// Print all initial k pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			int result = (3 *(n *n) - n) / 2;
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	PentagonalNumber *task = new PentagonalNumber();
	//  Pentagonal number are
	// —————————————————————————————————————————————
	//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
	//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
	//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
  
	// k = 10
	task->pentagonalNo(10);
	return 0;
}

Output

 1 5 12 22 35 51 70 92 117 145
// Include namespace system
using System;
// Csharp program for
// Pentagonal number
public class PentagonalNumber
{
	public void pentagonalNo(int k)
	{
		// Print all initial k pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			int result = (3 * (n * n) - n) / 2;
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		PentagonalNumber task = new PentagonalNumber();
		//  Pentagonal number are
		// —————————————————————————————————————————————
		//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
		//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
		//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
		//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
		//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
      
		// k = 10
		task.pentagonalNo(10);
	}
}

Output

 1 5 12 22 35 51 70 92 117 145
package main
import "fmt"
// Go program for
// Pentagonal number

func pentagonalNo(k int) {
	// Print all initial k pentagonal number
	for n := 1 ; n <= k ; n++ {
		// Formula
		//  (3n²-n)
		//  —————————
		//     2

		// Calculate nth pentagonal number
		var result int = (3 * (n * n) - n) / 2
		
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {

	//  Pentagonal number are
	// —————————————————————————————————————————————
	//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
	//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
	//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.

	// k = 10
	pentagonalNo(10)
}

Output

 1 5 12 22 35 51 70 92 117 145
<?php
// Php program for
// Pentagonal number
class PentagonalNumber
{
	public	function pentagonalNo($k)
	{
		// Print all initial k pentagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			$result = (int)((3 * ($n * $n) - $n) / 2);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new PentagonalNumber();
	//  Pentagonal number are
	// —————————————————————————————————————————————
	//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
	//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
	//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
  
	// k = 10
	$task->pentagonalNo(10);
}
main();

Output

 1 5 12 22 35 51 70 92 117 145
// Node JS program for
// Pentagonal number
class PentagonalNumber
{
	pentagonalNo(k)
	{
		// Print all initial k pentagonal number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			var result = parseInt((3 * (n * n) - n) / 2);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new PentagonalNumber();
	//  Pentagonal number are
	// —————————————————————————————————————————————
	//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
	//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
	//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
	// k = 10
	task.pentagonalNo(10);
}
main();

Output

 1 5 12 22 35 51 70 92 117 145
#  Python 3 program for
#  Pentagonal number
class PentagonalNumber :
	def pentagonalNo(self, k) :
		n = 1
		#  Print all initial k pentagonal number
		while (n <= k) :
			#  Formula
			#   (3n²-n)
			#   —————————
			#      2
            
			#  Calculate nth pentagonal number
			result = int((3 * (n * n) - n) / 2)

			#  Display calculated result
			print(" ", result, end = "")
			n += 1
		
	

def main() :
	task = PentagonalNumber()
	#   Pentagonal number are
	#  —————————————————————————————————————————————
	#   1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	#   210, 247, 287, 330, 376, 425, 477, 532, 590, 
	#   651, 715, 782, 852, 925, 1001, 1080, 1162, 
	#   1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	#   2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
    
	#  k = 10
	task.pentagonalNo(10)

if __name__ == "__main__": main()

Output

  1  5  12  22  35  51  70  92  117  145
#  Ruby program for
#  Pentagonal number
class PentagonalNumber 
	def pentagonalNo(k) 
		n = 1
		#  Print all initial k pentagonal number
		while (n <= k) 
			#  Formula
			#   (3n²-n)
			#   —————————
			#      2
            
			#  Calculate nth pentagonal number
			result = (3 * (n * n) - n) / 2

			#  Display calculated result
			print(" ", result)
			n += 1
		end

	end

end

def main() 
	task = PentagonalNumber.new()
	#   Pentagonal number are
	#  —————————————————————————————————————————————
	#   1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	#   210, 247, 287, 330, 376, 425, 477, 532, 590, 
	#   651, 715, 782, 852, 925, 1001, 1080, 1162, 
	#   1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	#   2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
    
	#  k = 10
	task.pentagonalNo(10)
end

main()

Output

 1 5 12 22 35 51 70 92 117 145
// Scala program for
// Pentagonal number
class PentagonalNumber()
{
	def pentagonalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k pentagonal number
		while (n <= k)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			var result: Int = (3 * (n * n) - n) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: PentagonalNumber = new PentagonalNumber();
		//  Pentagonal number are
		// —————————————————————————————————————————————
		//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
		//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
		//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
		//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
		//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
  
		// k = 10
		task.pentagonalNo(10);
	}
}

Output

 1 5 12 22 35 51 70 92 117 145
// Swift 4 program for
// Pentagonal number
class PentagonalNumber
{
	func pentagonalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k pentagonal number
		while (n <= k)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			let result: Int = (3 * (n * n) - n) / 2;
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: PentagonalNumber = PentagonalNumber();
	//  Pentagonal number are
	// —————————————————————————————————————————————
	//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
	//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
	//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
  
	// k = 10
	task.pentagonalNo(10);
}
main();

Output

  1  5  12  22  35  51  70  92  117  145
// Kotlin program for
// Pentagonal number
class PentagonalNumber
{
	fun pentagonalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k pentagonal number
		while (n <= k)
		{
			// Formula
			//  (3n²-n)
			//  —————————
			//     2
          
			// Calculate nth pentagonal number
			val result: Int = (3 * (n * n) - n) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: PentagonalNumber = PentagonalNumber();
	//  Pentagonal number are
	// —————————————————————————————————————————————
	//  1, 5, 12, 22, 35, 51, 70, 92, 117, 145, 176, 
	//  210, 247, 287, 330, 376, 425, 477, 532, 590, 
	//  651, 715, 782, 852, 925, 1001, 1080, 1162, 
	//  1247, 1335, 1426, 1520, 1617, 1717, 1820, 1926, 
	//  2035, 2147, 2262, 2380, 2501, 2625, 2752, 2882 etc.
  
	// k = 10
	task.pentagonalNo(10);
}

Output

 1 5 12 22 35 51 70 92 117 145


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