Triangular number

Here given code implementation process.

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

void triangularNo(int k)
{
	// Print all initial k triangular number
	for (int n = 0; n < k; ++n)
	{
		// Formula
		//   n(n + 1) 
		//  ——————————
		//      2
      
		// Calculate nth triangular number
		int result = (n *(n + 1)) / 2;
      
		// Display calculated result
		printf("  %d", result);
	}
}
int main()
{
	// Triangular number are
	// —————————————————————————————————————————————
	// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	// 276, 300, 325, 351, 378, 406, 435, 465, 
	// 496, 528, 561, 595 ... etc
  
	// k = 10
	triangularNo(10);
	return 0;
}

Output

  0  1  3  6  10  15  21  28  36  45
// Java program for
// Triangular number
public class TriangularNumber
{
    public void triangularNo(int k)
    {
        // Print all initial k triangular number
        for (int n = 0; n < k; ++n)
        {
            // Formula
            //   n(n + 1) 
            //  ——————————
            //      2

            // Calculate nth triangular number
            int result = (n * (n + 1)) / 2;
            
            // Display calculated result
            System.out.print(" " + result );
        }
    }
    public static void main(String[] args)
    {
        TriangularNumber task = new TriangularNumber();
        // Triangular number are
        // —————————————————————————————————————————————
        // 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
        // 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
        // 276, 300, 325, 351, 378, 406, 435, 465, 
        // 496, 528, 561, 595 ... etc
      
        // k = 10
        task.triangularNo(10);
    }
}

Output

 0 1 3 6 10 15 21 28 36 45
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Triangular number

class TriangularNumber
{
	public: void triangularNo(int k)
	{
		// Print all initial k triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//   n(n + 1) 
			//  ——————————
			//      2
          
			// Calculate nth triangular number
			int result = (n *(n + 1)) / 2;
          
			// Display calculated result
			cout << " " << result;
		}
	}
};
int main()
{
	TriangularNumber *task = new TriangularNumber();
	// Triangular number are
	// —————————————————————————————————————————————
	// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	// 276, 300, 325, 351, 378, 406, 435, 465, 
	// 496, 528, 561, 595 ... etc
  
	// k = 10
	task->triangularNo(10);
	return 0;
}

Output

 0 1 3 6 10 15 21 28 36 45
// Include namespace system
using System;
// Csharp program for
// Triangular number
public class TriangularNumber
{
	public void triangularNo(int k)
	{
		// Print all initial k triangular number
		for (int n = 0; n < k; ++n)
		{
			// Formula
			//   n(n + 1) 
			//  ——————————
			//      2
          
			// Calculate nth triangular number
			int result = (n * (n + 1)) / 2;
          
			// Display calculated result
			Console.Write(" " + result);
		}
	}
	public static void Main(String[] args)
	{
		TriangularNumber task = new TriangularNumber();
		// Triangular number are
		// —————————————————————————————————————————————
		// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
		// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
		// 276, 300, 325, 351, 378, 406, 435, 465, 
		// 496, 528, 561, 595 ... etc
      
		// k = 10
		task.triangularNo(10);
	}
}

Output

 0 1 3 6 10 15 21 28 36 45
package main
import "fmt"
// Go program for
// Triangular number

func triangularNo(k int) {
	// Print all initial k triangular number
	for n := 0 ; n < k ; n++ {
		// Formula
		//   n(n + 1) 
		//  ——————————
		//      2

		// Calculate nth triangular number
		var result int = (n * (n + 1)) / 2

		// Display calculated result
		fmt.Print(" ", result)
	}
}
func main() {
	var task * TriangularNumber = getTriangularNumber()
	// Triangular number are
	// —————————————————————————————————————————————
	// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	// 276, 300, 325, 351, 378, 406, 435, 465, 
	// 496, 528, 561, 595 ... etc
	
	// k = 10
	task.triangularNo(10)
}

Output

 0 1 3 6 10 15 21 28 36 45
<?php
// Php program for
// Triangular number
class TriangularNumber
{
	public	function triangularNo($k)
	{
		// Print all initial k triangular number
		for ($n = 0; $n < $k; ++$n)
		{
			// Formula
			//   n(n + 1) 
			//  ——————————
			//      2
          
			// Calculate nth triangular number
			$result = (int)(($n * ($n + 1)) / 2);
          
			// Display calculated result
			echo(" ".$result);
		}
	}
}

function main()
{
	$task = new TriangularNumber();
	// Triangular number are
	// —————————————————————————————————————————————
	// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	// 276, 300, 325, 351, 378, 406, 435, 465, 
	// 496, 528, 561, 595 ... etc
  
	// k = 10
	$task->triangularNo(10);
}
main();

Output

 0 1 3 6 10 15 21 28 36 45
// Node JS program for
// Triangular number
class TriangularNumber
{
	triangularNo(k)
	{
		// Print all initial k triangular number
		for (var n = 0; n < k; ++n)
		{
			// Formula
			//   n(n + 1) 
			//  ——————————
			//      2
          
			// Calculate nth triangular number
			var result = parseInt((n * (n + 1)) / 2);
          
			// Display calculated result
			process.stdout.write(" " + result);
		}
	}
}

function main()
{
	var task = new TriangularNumber();
	// Triangular number are
	// —————————————————————————————————————————————
	// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	// 276, 300, 325, 351, 378, 406, 435, 465, 
	// 496, 528, 561, 595 ... etc
  
	// k = 10
	task.triangularNo(10);
}
main();

Output

 0 1 3 6 10 15 21 28 36 45
#  Python 3 program for
#  Triangular number
class TriangularNumber :
	def triangularNo(self, k) :
		n = 0
		#  Print all initial k triangular number
		while (n < k) :
			#  Formula
			#    n(n + 1) 
			#   ——————————
			#       2
            
			#  Calculate nth triangular number
			result = int((n * (n + 1)) / 2)

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

def main() :
	task = TriangularNumber()
	#  Triangular number are
	#  —————————————————————————————————————————————
	#  0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	#  91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	#  276, 300, 325, 351, 378, 406, 435, 465, 
	#  496, 528, 561, 595 ... etc
    
	#  k = 10
	task.triangularNo(10)

if __name__ == "__main__": main()

Output

  0  1  3  6  10  15  21  28  36  45
#  Ruby program for
#  Triangular number
class TriangularNumber 
	def triangularNo(k) 
		n = 0
		#  Print all initial k triangular number
		while (n < k) 
			#  Formula
			#    n(n + 1) 
			#   ——————————
			#       2
            
			#  Calculate nth triangular number
			result = (n * (n + 1)) / 2

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

	end

end

def main() 
	task = TriangularNumber.new()
	#  Triangular number are
	#  —————————————————————————————————————————————
	#  0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	#  91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	#  276, 300, 325, 351, 378, 406, 435, 465, 
	#  496, 528, 561, 595 ... etc
    
	#  k = 10
	task.triangularNo(10)
end

main()

Output

 0 1 3 6 10 15 21 28 36 45
// Scala program for
// Triangular number
class TriangularNumber()
{
	def triangularNo(k: Int): Unit = {
		var n: Int = 0;
		// Print all initial k triangular number
		while (n < k)
		{
			// Formula
			//   n(n + 1) 
			//  ——————————
			//      2
          
			// Calculate nth triangular number
			var result: Int = (n * (n + 1)) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: TriangularNumber = new TriangularNumber();
		// Triangular number are
		// —————————————————————————————————————————————
		// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
		// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
		// 276, 300, 325, 351, 378, 406, 435, 465, 
		// 496, 528, 561, 595 ... etc
  
		// k = 10
		task.triangularNo(10);
	}
}

Output

 0 1 3 6 10 15 21 28 36 45
// Swift 4 program for
// Triangular number
class TriangularNumber
{
	func triangularNo(_ k: Int)
	{
		var n: Int = 0;
		// Print all initial k triangular number
		while (n < k)
		{
			// Formula
			//   n(n + 1) 
			//  ——————————
			//      2
          
			// Calculate nth triangular number
			let result: Int = (n * (n + 1)) / 2;
          
			// Display calculated result
			print(" ", result, terminator: "");
			n += 1;
		}
	}
}
func main()
{
	let task: TriangularNumber = TriangularNumber();
	// Triangular number are
	// —————————————————————————————————————————————
	// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	// 276, 300, 325, 351, 378, 406, 435, 465, 
	// 496, 528, 561, 595 ... etc
  
	// k = 10
	task.triangularNo(10);
}
main();

Output

  0  1  3  6  10  15  21  28  36  45
// Kotlin program for
// Triangular number
class TriangularNumber
{
	fun triangularNo(k: Int): Unit
	{
		var n: Int = 0;
		// Print all initial k triangular number
		while (n < k)
		{
			// Formula
			//   n(n + 1) 
			//  ——————————
			//      2
          
			// Calculate nth triangular number
			val result: Int = (n * (n + 1)) / 2;
          
			// Display calculated result
			print(" " + result);
			n += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: TriangularNumber = TriangularNumber();
	// Triangular number are
	// —————————————————————————————————————————————
	// 0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 
	// 91, 105, 120, 136, 153, 171, 190, 210, 231, 253, 
	// 276, 300, 325, 351, 378, 406, 435, 465, 
	// 496, 528, 561, 595 ... etc
  
	// k = 10
	task.triangularNo(10);
}

Output

 0 1 3 6 10 15 21 28 36 45


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