Octagonal number

Here given code implementation process.

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

void octagonalNo(int k)
{
	// Print all initial k octagonal number
	for (int n = 1; n <= k; ++n)
	{
		// Formula
		//  (3n²-2n)
      
		// Calculate nth octagonal number
		int result = (3 *(n *n) - 2 *n);
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
    // 4256, 4485, 4720 etc.
	// k = 10
	octagonalNo(10);
	return 0;
}

Output

  1  8  21  40  65  96  133  176  225  280
// Java program for
// Octagonal number
public class OctagonalNumber
{
	public void octagonalNo(int k)
	{
		// Print all initial k octagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			int result = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			System.out.print(" " + result);
		}
	}
	public static void main(String[] args)
	{
		OctagonalNumber task = new OctagonalNumber();
		//  Octagonal number are
		// —————————————————————————————————————————————
		//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
		// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
		// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
		// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
		// 4256, 4485, 4720 etc.
      
		// k = 10
		task.octagonalNo(10);
	}
}

Output

 1 8 21 40 65 96 133 176 225 280
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Octagonal number

class OctagonalNumber
{
	public: void octagonalNo(int k)
	{
		// Print all initial k octagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			int result = (3 *(n *n) - 2 *n);
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	OctagonalNumber *task = new OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
  
	// k = 10
	task->octagonalNo(10);
	return 0;
}

Output

 1 8 21 40 65 96 133 176 225 280
// Include namespace system
using System;
// Csharp program for
// Octagonal number
public class OctagonalNumber
{
	public void octagonalNo(int k)
	{
		// Print all initial k octagonal number
		for (int n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			int result = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		OctagonalNumber task = new OctagonalNumber();
		//  Octagonal number are
		// —————————————————————————————————————————————
		//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
		// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
		// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
		// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
		// 4256, 4485, 4720 etc.
      
		// k = 10
		task.octagonalNo(10);
	}
}

Output

 1 8 21 40 65 96 133 176 225 280
package main
import "fmt"
// Go program for
// Octagonal number

func octagonalNo(k int) {
	// Print all initial k octagonal number
	for n := 1 ; n <= k ; n++ {
		// Formula
		// (3n²-2n)
		// Calculate nth octagonal number
		var result int = (3 * (n * n) - 2 * n)
		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.

	// k = 10
	octagonalNo(10)
}

Output

 1 8 21 40 65 96 133 176 225 280
<?php
// Php program for
// Octagonal number
class OctagonalNumber
{
	public	function octagonalNo($k)
	{
		// Print all initial k octagonal number
		for ($n = 1; $n <= $k; ++$n)
		{
			// Formula
			// (3n²-2n)
			// Calculate nth octagonal number
			$result = (3 * ($n * $n) - 2 * $n);
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	$task->octagonalNo(10);
}
main();

Output

 1 8 21 40 65 96 133 176 225 280
// Node JS program for
// Octagonal number
class OctagonalNumber
{
	octagonalNo(k)
	{
		// Print all initial k octagonal number
		for (var n = 1; n <= k; ++n)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			var result = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	task.octagonalNo(10);
}
main();

Output

 1 8 21 40 65 96 133 176 225 280
#  Python 3 program for
#  Octagonal number
class OctagonalNumber :
	def octagonalNo(self, k) :
		n = 1
		#  Print all initial k octagonal number
		while (n <= k) :
			#  Formula
			#  (3n²-2n)
            
			#  Calculate nth octagonal number
			result = (3 * (n * n) - 2 * n)

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

def main() :
	task = OctagonalNumber()
	#   Octagonal number are
	#  —————————————————————————————————————————————
	#   1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	#  341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	#  1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	#  2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	#  4256, 4485, 4720 etc.
	#  k = 10
	task.octagonalNo(10)

if __name__ == "__main__": main()

Output

  1  8  21  40  65  96  133  176  225  280
#  Ruby program for
#  Octagonal number
class OctagonalNumber 
	def octagonalNo(k) 
		n = 1
		#  Print all initial k octagonal number
		while (n <= k) 
			#  Formula
			#  (3n²-2n)
            
			#  Calculate nth octagonal number
			result = (3 * (n * n) - 2 * n)

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

	end

end

def main() 
	task = OctagonalNumber.new()
	#   Octagonal number are
	#  —————————————————————————————————————————————
	#   1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	#  341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	#  1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	#  2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	#  4256, 4485, 4720 etc.
    
	#  k = 10
	task.octagonalNo(10)
end

main()

Output

 1 8 21 40 65 96 133 176 225 280
// Scala program for
// Octagonal number
class OctagonalNumber()
{
	def octagonalNo(k: Int): Unit = {
		var n: Int = 1;
		// Print all initial k octagonal number
		while (n <= k)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			var result: Int = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: OctagonalNumber = new OctagonalNumber();
		//  Octagonal number are
		// —————————————————————————————————————————————
		//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
		// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
		// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
		// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
		// 4256, 4485, 4720 etc.
  
		// k = 10
		task.octagonalNo(10);
	}
}

Output

 1 8 21 40 65 96 133 176 225 280
// Swift 4 program for
// Octagonal number
class OctagonalNumber
{
	func octagonalNo(_ k: Int)
	{
		var n: Int = 1;
		// Print all initial k octagonal number
		while (n <= k)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			let result: Int = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: OctagonalNumber = OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	task.octagonalNo(10);
}
main();

Output

  1  8  21  40  65  96  133  176  225  280
// Kotlin program for
// Octagonal number
class OctagonalNumber
{
	fun octagonalNo(k: Int): Unit
	{
		var n: Int = 1;
		// Print all initial k octagonal number
		while (n <= k)
		{
			// Formula
			// (3n²-2n)
          
			// Calculate nth octagonal number
			val result: Int = (3 * (n * n) - 2 * n);
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: OctagonalNumber = OctagonalNumber();
	//  Octagonal number are
	// —————————————————————————————————————————————
	//  1, 8, 21, 40, 65, 96, 133, 176, 225, 280, 
	// 341, 408, 481, 560, 645, 736, 833, 936, 1045, 1160, 
	// 1281, 1408, 1541, 1680, 1825, 1976, 2133, 2296, 2465, 
	// 2640, 2821, 3008, 3201, 3400, 3605, 3816, 4033, 
	// 4256, 4485, 4720 etc.
	// k = 10
	task.octagonalNo(10);
}

Output

 1 8 21 40 65 96 133 176 225 280


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