Centered hexagonal numbers

Here given code implementation process.

// C Program for
// Centered hexagonal numbers
#include <stdio.h>

void centeredHexagonalNo(int k)
{
	int result = 0;
  
	// Print all initial k centered hexagonal number
	for (int n = 1; n <= k; ++n)
	{
		// Centered hexagonal 
		// formula =  3n(n - 1) + 1
      
      	// Calculated centered hexagonal 
		result = ((3 *n) *(n - 1) + 1);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
  
	// Test 
	// k  = 10
	centeredHexagonalNo(10);
	return 0;
}

Output

  1  7  19  37  61  91  127  169  217  271
// Java program for
// Centered hexagonal numbers
public class Hexagonal
{
    public void centeredHexagonalNo(int k)
    {
        int result = 0;
        // Print all initial k centered hexagonal number
        for (int n = 1; n <= k; ++n)
        {
            // Centered hexagonal 
            // formula =  3n(n - 1) + 1

            // Calculated centered hexagonal 
            result = ((3 * n) * (n - 1) + 1);
            
            // Display calculated result
            System.out.print(" " + result );
        }
    }
    public static void main(String[] args)
    {
        Hexagonal task = new Hexagonal();
        // Centered hexagonal numbers
        // --------------------------------------
        // 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
        // 397, 469, 547, 631, 721, 817, 919 .. etc
        // Test 
        // k  = 10
        task.centeredHexagonalNo(10);
    }
}

Output

 1 7 19 37 61 91 127 169 217 271
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered hexagonal numbers

class Hexagonal
{
	public: void centeredHexagonalNo(int k)
	{
		int result = 0;
		// Print all initial k centered hexagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 *n) *(n - 1) + 1);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	Hexagonal *task = new Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	task->centeredHexagonalNo(10);
	return 0;
}

Output

 1 7 19 37 61 91 127 169 217 271
// Include namespace system
using System;
// Csharp program for
// Centered hexagonal numbers
public class Hexagonal
{
	public void centeredHexagonalNo(int k)
	{
		int result = 0;
		// Print all initial k centered hexagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		Hexagonal task = new Hexagonal();
		// Centered hexagonal numbers
		// --------------------------------------
		// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
		// 397, 469, 547, 631, 721, 817, 919 .. etc
		// Test 
		// k  = 10
		task.centeredHexagonalNo(10);
	}
}

Output

 1 7 19 37 61 91 127 169 217 271
package main
import "fmt"
// Go program for
// Centered hexagonal numbers

func centeredHexagonalNo(k int) {
	var result int = 0
	// Print all initial k centered hexagonal number
	for n := 1 ; n <= k ; n++ {
		// Centered hexagonal 
		// formula =  3n(n - 1) + 1

		// Calculated centered hexagonal 
		result = ((3 * n) * (n - 1) + 1)
		
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc

	// Test 
	// k  = 10
	centeredHexagonalNo(10)
}

Output

 1 7 19 37 61 91 127 169 217 271
<?php
// Php program for
// Centered hexagonal numbers
class Hexagonal
{
	public	function centeredHexagonalNo($k)
	{
		$result = 0;
		// Print all initial k centered hexagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			$result = ((3 * $n) * ($n - 1) + 1);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	$task->centeredHexagonalNo(10);
}
main();

Output

 1 7 19 37 61 91 127 169 217 271
// Node JS program for
// Centered hexagonal numbers
class Hexagonal
{
	centeredHexagonalNo(k)
	{
		var result = 0;
		// Print all initial k centered hexagonal number
		for (var n = 1; n <= k; ++n)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	task.centeredHexagonalNo(10);
}
main();

Output

 1 7 19 37 61 91 127 169 217 271
#  Python 3 program for
#  Centered hexagonal numbers
class Hexagonal :
	def centeredHexagonalNo(self, k) :
		result = 0
		n = 1
		#  Print all initial k centered hexagonal number
		while (n <= k) :
			#  Centered hexagonal 
			#  formula =  3n(n - 1) + 1
            
			#  Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1)

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

def main() :
	task = Hexagonal()
	#  Centered hexagonal numbers
	#  --------------------------------------
	#  1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	#  397, 469, 547, 631, 721, 817, 919 .. etc
    
	#  Test 
	#  k  = 10
	task.centeredHexagonalNo(10)

if __name__ == "__main__": main()

Output

  1  7  19  37  61  91  127  169  217  271
#  Ruby program for
#  Centered hexagonal numbers
class Hexagonal 
	def centeredHexagonalNo(k) 
		result = 0
		n = 1
		#  Print all initial k centered hexagonal number
		while (n <= k) 
			#  Centered hexagonal 
			#  formula =  3n(n - 1) + 1
            
			#  Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1)

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

	end

end

def main() 
	task = Hexagonal.new()
	#  Centered hexagonal numbers
	#  --------------------------------------
	#  1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	#  397, 469, 547, 631, 721, 817, 919 .. etc
    
	#  Test 
	#  k  = 10
	task.centeredHexagonalNo(10)
end

main()

Output

 1 7 19 37 61 91 127 169 217 271
// Scala program for
// Centered hexagonal numbers
class Hexagonal()
{
	def centeredHexagonalNo(k: Int): Unit = {
		var result: Int = 0;
		var n: Int = 1;
		// Print all initial k centered hexagonal number
		while (n <= k)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Hexagonal = new Hexagonal();
		// Centered hexagonal numbers
		// --------------------------------------
		// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
		// 397, 469, 547, 631, 721, 817, 919 .. etc
  
		// Test 
		// k  = 10
		task.centeredHexagonalNo(10);
	}
}

Output

 1 7 19 37 61 91 127 169 217 271
// Swift 4 program for
// Centered hexagonal numbers
class Hexagonal
{
	func centeredHexagonalNo(_ k: Int)
	{
		var result: Int = 0;
		var n: Int = 1;
		// Print all initial k centered hexagonal number
		while (n <= k)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: Hexagonal = Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
	// Test 
	// k  = 10
	task.centeredHexagonalNo(10);
}
main();

Output

  1  7  19  37  61  91  127  169  217  271
// Kotlin program for
// Centered hexagonal numbers
class Hexagonal
{
	fun centeredHexagonalNo(k: Int): Unit
	{
		var result: Int;
		var n: Int = 1;
		// Print all initial k centered hexagonal number
		while (n <= k)
		{
			// Centered hexagonal 
			// formula =  3n(n - 1) + 1
          
			// Calculated centered hexagonal 
			result = ((3 * n) * (n - 1) + 1);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Hexagonal = Hexagonal();
	// Centered hexagonal numbers
	// --------------------------------------
	// 1, 7, 19, 37, 61, 91, 127, 169, 217, 271, 331, 
	// 397, 469, 547, 631, 721, 817, 919 .. etc
  
	// Test 
	// k  = 10
	task.centeredHexagonalNo(10);
}

Output

 1 7 19 37 61 91 127 169 217 271


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