Centered pentagonal number

Here given code implementation process.

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

void centeredPentagonalNo(int k)
{
	// Print all initial k centered pentagonal number
	for (int n = 1; n <= k; ++n)
	{
		//   Formula
		//   (5n² - 5n + 2) 
		//   ———————————————
		//        5
      
		// Calculate nth pentagonal number
		int result = (5 *(n *n) - 5 *n + 2) / 2;
      
		// Display Calculated value
		printf("  %d", result);
	}
}
int main()
{
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
	//  Test  k = 10
	centeredPentagonalNo(10);
	return 0;
}

Output

  1  6  16  31  51  76  106  141  181  226
// Java program for
// Centered pentagonal number
public class CenteredPentagonal
{
	public void centeredPentagonalNo(int k)
	{
		// Print all initial k centered pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			int result = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		CenteredPentagonal task = new CenteredPentagonal();
		// Centered pentagonal numbers are
		// —————————————————————————————————————————————
		// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
		// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
		// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
      
		//  Test  k = 10
		task.centeredPentagonalNo(10);
	}
}

Output

 1 6 16 31 51 76 106 141 181 226
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered pentagonal number

class CenteredPentagonal
{
	public: void centeredPentagonalNo(int k)
	{
		// Print all initial k centered pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			int result = (5 *(n *n) - 5 *n + 2) / 2;
          
			// Display Calculated value
			cout << " " << result;
		}
	}
};
int main()
{
	CenteredPentagonal *task = new CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	//  Test  k = 10
	task->centeredPentagonalNo(10);
	return 0;
}

Output

 1 6 16 31 51 76 106 141 181 226
// Include namespace system
using System;
// Csharp program for
// Centered pentagonal number
public class CenteredPentagonal
{
	public void centeredPentagonalNo(int k)
	{
		// Print all initial k centered pentagonal number
		for (int n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			int result = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		CenteredPentagonal task = new CenteredPentagonal();
		// Centered pentagonal numbers are
		// —————————————————————————————————————————————
		// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
		// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
		// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
      
		//  Test  k = 10
		task.centeredPentagonalNo(10);
	}
}

Output

 1 6 16 31 51 76 106 141 181 226
package main
import "fmt"
// Go program for
// Centered pentagonal number

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

		// Calculate nth pentagonal number
		var result int = (5 * (n * n) - 5 * n + 2) / 2

		// Display Calculated value
		fmt.Print(" ", result)
	}
}
func main() {
	
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	
	//  Test  k = 10
	centeredPentagonalNo(10)
}

Output

 1 6 16 31 51 76 106 141 181 226
<?php
// Php program for
// Centered pentagonal number
class CenteredPentagonal
{
	public	function centeredPentagonalNo($k)
	{
		// Print all initial k centered pentagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			$result = (int)((5 * ($n * $n) - 5 * $n + 2) / 2);
          
			// Display Calculated value
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
	//  Test  k = 10
	$task->centeredPentagonalNo(10);
}
main();

Output

 1 6 16 31 51 76 106 141 181 226
// Node JS program for
// Centered pentagonal number
class CenteredPentagonal
{
	centeredPentagonalNo(k)
	{
		// Print all initial k centered pentagonal number
		for (var n = 1; n <= k; ++n)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			var result = parseInt((5 * (n * n) - 5 * n + 2) / 2);
          
			// Display Calculated value
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
	//  Test  k = 10
	task.centeredPentagonalNo(10);
}
main();

Output

 1 6 16 31 51 76 106 141 181 226
#  Python 3 program for
#  Centered pentagonal number
class CenteredPentagonal :
	def centeredPentagonalNo(self, k) :
		n = 1
		#  Print all initial k centered pentagonal number
		while (n <= k) :
			#    Formula
			#    (5n² - 5n + 2) 
			#    ———————————————
			#         5
            
			#  Calculate nth pentagonal number
			result = int((5 * (n * n) - 5 * n + 2) / 2)

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

def main() :
	task = CenteredPentagonal()
	#  Centered pentagonal numbers are
	#  —————————————————————————————————————————————
	#  1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	#  331, 391, 456, 526, 601, 681, 766, 856, 951, 
	#  1051, 1156, 1266, 1381, 1501, 1626 ... etc
    
	#   Test  k = 10
	task.centeredPentagonalNo(10)

if __name__ == "__main__": main()

Output

  1  6  16  31  51  76  106  141  181  226
#  Ruby program for
#  Centered pentagonal number
class CenteredPentagonal 
	def centeredPentagonalNo(k) 
		n = 1
		#  Print all initial k centered pentagonal number
		while (n <= k) 
			#    Formula
			#    (5n² - 5n + 2) 
			#    ———————————————
			#         5
            
			#  Calculate nth pentagonal number
			result = (5 * (n * n) - 5 * n + 2) / 2

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

	end

end

def main() 
	task = CenteredPentagonal.new()
	#  Centered pentagonal numbers are
	#  —————————————————————————————————————————————
	#  1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	#  331, 391, 456, 526, 601, 681, 766, 856, 951, 
	#  1051, 1156, 1266, 1381, 1501, 1626 ... etc
    
	#   Test  k = 10
	task.centeredPentagonalNo(10)
end

main()

Output

 1 6 16 31 51 76 106 141 181 226
// Scala program for
// Centered pentagonal number
class CenteredPentagonal()
{
	def centeredPentagonalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k centered pentagonal number
		while (n <= k)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			var result: Int = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: CenteredPentagonal = new CenteredPentagonal();
		// Centered pentagonal numbers are
		// —————————————————————————————————————————————
		// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
		// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
		// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
  
		//  Test  k = 10
		task.centeredPentagonalNo(10);
	}
}

Output

 1 6 16 31 51 76 106 141 181 226
// Swift 4 program for
// Centered pentagonal number
class CenteredPentagonal
{
	func centeredPentagonalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k centered pentagonal number
		while (n <= k)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			let result: Int = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: CenteredPentagonal = CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	//  Test  k = 10
	task.centeredPentagonalNo(10);
}
main();

Output

  1  6  16  31  51  76  106  141  181  226
// Kotlin program for
// Centered pentagonal number
class CenteredPentagonal
{
	fun centeredPentagonalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k centered pentagonal number
		while (n <= k)
		{
			//   Formula
			//   (5n² - 5n + 2) 
			//   ———————————————
			//        5
          
			// Calculate nth pentagonal number
			val result: Int = (5 * (n * n) - 5 * n + 2) / 2;
          
			// Display Calculated value
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: CenteredPentagonal = CenteredPentagonal();
	// Centered pentagonal numbers are
	// —————————————————————————————————————————————
	// 1, 6, 16, 31, 51, 76, 106, 141, 181, 226, 276, 
	// 331, 391, 456, 526, 601, 681, 766, 856, 951, 
	// 1051, 1156, 1266, 1381, 1501, 1626 ... etc
	//  Test  k = 10
	task.centeredPentagonalNo(10);
}

Output

 1 6 16 31 51 76 106 141 181 226


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