Centered triangular number

Here given code implementation process.

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

void centeredTriangularNo(int k)
{
	// Print all initial k centered triangular number
	for (int n = 0; n < k; ++n)
	{
		// Formula
		//  (3n²) + 3n + 1
		// —————————————
		//       2
      
		// Calculate nth triangular number
		int result = ((3 *(n *n) + (3 *n) + 2) / 2);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	centeredTriangularNo(10);
	return 0;
}

Output

  1  4  10  19  31  46  64  85  109  136
// Java program for
// Centered triangular number
public class TriangularNumber
{
	public void centeredTriangularNo(int k)
	{
		// Print all initial k centered triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			int result = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		TriangularNumber task = new TriangularNumber();
		// Centered triangular number are
		// —————————————————————————————————————————————
		// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
		// 199, 235, 274, 316, 361, 409, 460, 514, 
		// 571, 631, 694, 760, 829 ... etc
      
		// k = 10
		task.centeredTriangularNo(10);
	}
}

Output

 1 4 10 19 31 46 64 85 109 136
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Centered triangular number
class TriangularNumber
{
	public: void centeredTriangularNo(int k)
	{
		// Print all initial k centered triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			int result = ((3 *(n *n) + (3 *n) + 2) / 2);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	TriangularNumber *task = new TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task->centeredTriangularNo(10);
	return 0;
}

Output

 1 4 10 19 31 46 64 85 109 136
// Include namespace system
using System;
// Csharp program for
// Centered triangular number
public class TriangularNumber
{
	public void centeredTriangularNo(int k)
	{
		// Print all initial k centered triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			int result = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		TriangularNumber task = new TriangularNumber();
		// Centered triangular number are
		// —————————————————————————————————————————————
		// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
		// 199, 235, 274, 316, 361, 409, 460, 514, 
		// 571, 631, 694, 760, 829 ... etc
      
		// k = 10
		task.centeredTriangularNo(10);
	}
}

Output

 1 4 10 19 31 46 64 85 109 136
package main
import "fmt"
// Go program for
// Centered triangular number

func centeredTriangularNo(k int) {
	// Print all initial k centered triangular number
	for n := 0 ; n < k ; n++ {
		// Formula
		//  (3n²) + 3n + 1
		// —————————————
		//       2
		// Calculate nth triangular number
		var result int = ((3 * (n * n) + (3 * n) + 2) / 2)
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	centeredTriangularNo(10)
}

Output

 1 4 10 19 31 46 64 85 109 136
<?php
// Php program for
// Centered triangular number
class TriangularNumber
{
	public	function centeredTriangularNo($k)
	{
		// Print all initial k centered triangular number
		for ($n = 0; $n < $k; ++$n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			$result = ((int)((3 * ($n * $n) + (3 * $n) + 2) / 2));
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	$task->centeredTriangularNo(10);
}
main();

Output

 1 4 10 19 31 46 64 85 109 136
// Node JS program for
// Centered triangular number
class TriangularNumber
{
	centeredTriangularNo(k)
	{
		// Print all initial k centered triangular number
		for (var n = 0; n < k; ++n)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			var result = (parseInt((3 * (n * n) + (3 * n) + 2) / 2));
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task.centeredTriangularNo(10);
}
main();

Output

 1 4 10 19 31 46 64 85 109 136
#  Python 3 program for
#  Centered triangular number
class TriangularNumber :
	def centeredTriangularNo(self, k) :
		n = 0
		#  Print all initial k centered triangular number
		while (n < k) :
			#  Formula
			#   (3n²) + 3n + 1
			#  —————————————
			#        2
            
			#  Calculate nth triangular number
			result = (int((3 * (n * n) + (3 * n) + 2) / 2))

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

def main() :
	task = TriangularNumber()
	#  Centered triangular number are
	#  —————————————————————————————————————————————
	#  1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	#  199, 235, 274, 316, 361, 409, 460, 514, 
	#  571, 631, 694, 760, 829 ... etc
    
	#  k = 10
	task.centeredTriangularNo(10)

if __name__ == "__main__": main()

Output

  1  4  10  19  31  46  64  85  109  136
#  Ruby program for
#  Centered triangular number
class TriangularNumber 
	def centeredTriangularNo(k) 
		n = 0
		#  Print all initial k centered triangular number
		while (n < k) 
			#  Formula
			#   (3n²) + 3n + 1
			#  —————————————
			#        2
            
			#  Calculate nth triangular number
			result = ((3 * (n * n) + (3 * n) + 2) / 2)

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

	end

end

def main() 
	task = TriangularNumber.new()
	#  Centered triangular number are
	#  —————————————————————————————————————————————
	#  1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	#  199, 235, 274, 316, 361, 409, 460, 514, 
	#  571, 631, 694, 760, 829 ... etc
    
	#  k = 10
	task.centeredTriangularNo(10)
end

main()

Output

 1 4 10 19 31 46 64 85 109 136
// Scala program for
// Centered triangular number
class TriangularNumber()
{
	def centeredTriangularNo(k: Int): Unit = {
		var n: Int = 0;
		// Print all initial k centered triangular number
		while (n < k)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			var result: Int = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: TriangularNumber = new TriangularNumber();
		// Centered triangular number are
		// —————————————————————————————————————————————
		// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
		// 199, 235, 274, 316, 361, 409, 460, 514, 
		// 571, 631, 694, 760, 829 ... etc
		// k = 10
		task.centeredTriangularNo(10);
	}
}

Output

 1 4 10 19 31 46 64 85 109 136
// Swift 4 program for
// Centered triangular number
class TriangularNumber
{
	func centeredTriangularNo(_ k: Int)
	{
		var n: Int = 0;
		// Print all initial k centered triangular number
		while (n < k)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			let result: Int = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: TriangularNumber = TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task.centeredTriangularNo(10);
}
main();

Output

  1  4  10  19  31  46  64  85  109  136
// Kotlin program for
// Centered triangular number
class TriangularNumber
{
	fun centeredTriangularNo(k: Int): Unit
	{
		var n: Int = 0;
		// Print all initial k centered triangular number
		while (n < k)
		{
			// Formula
			//  (3n²) + 3n + 1
			// —————————————
			//       2
          
			// Calculate nth triangular number
			val result: Int = ((3 * (n * n) + (3 * n) + 2) / 2);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: TriangularNumber = TriangularNumber();
	// Centered triangular number are
	// —————————————————————————————————————————————
	// 1, 4, 10, 19, 31, 46, 64, 85, 109, 136, 166, 
	// 199, 235, 274, 316, 361, 409, 460, 514, 
	// 571, 631, 694, 760, 829 ... etc
  
	// k = 10
	task.centeredTriangularNo(10);
}

Output

 1 4 10 19 31 46 64 85 109 136


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