Posted on by Kalkicode
Code Number

Triangular number

In mathematics, a triangular number is a number that can be represented in the form of a triangular grid of dots. The nth triangular number is the number of dots in the triangular arrangement with n dots on a side, and it can be calculated using a simple formula.

Problem Statement

The task is to write a program that prints the first k triangular numbers. Triangular numbers are a sequence of numbers that can be represented by arranging dots in the shape of an equilateral triangle. The program should take an input value k and output the first k triangular numbers.

For example, if k = 10, the program should print the following triangular numbers: 0, 1, 3, 6, 10, 15, 21, 28, 36, 45.

Explanation

The given code is a C program that calculates and prints the first k triangular numbers using a loop. It defines a function named triangularNo that takes an integer parameter k. Inside the function, a loop is used to iterate from 0 to k-1. For each iteration, the current index n is used in the formula (n * (n + 1)) / 2 to calculate the nth triangular number.

The formula to calculate the nth triangular number is derived from the fact that the sum of the first n natural numbers is given by the formula n * (n + 1) / 2. Since each triangular number represents the sum of consecutive natural numbers, this formula can be used to calculate the triangular numbers efficiently.

The calculated triangular number is then printed using the printf function. The loop continues until all k triangular numbers are printed.

Algorithm

The algorithm for calculating the first k triangular numbers can be summarized as follows:

  1. Define a function triangularNo that takes an integer parameter k.
  2. Initialize a loop variable n to 0.
  3. Iterate the loop until n < k.
    1. Calculate the nth triangular number using the formula (n * (n + 1)) / 2.
    2. Print the calculated triangular number.
    3. Increment n by 1.
  4. End the loop.

Pseudocode

function triangularNo(k):
    for n = 0 to k-1 do:
        result = (n * (n + 1)) / 2
        print result
    end for
end function
main():
triangularNo(10)
end main

Code Solution

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

Output Explanation

When the code is executed with k = 10, the triangularNo function is called with the value 10 as an argument. The function then iterates the loop 10 times, calculating and printing the first 10 triangular numbers. The calculated triangular numbers are: 0, 1, 3, 6, 10, 15, 21, 28, 36, 45.

Each number represents the total number of dots required to form a triangular arrangement of that size. For example, the first triangular number 0 represents an empty triangle with 0 dots. The second triangular number 1 represents a triangle with 1 dot, and so on.

Time Complexity

The time complexity of the given code is O(k) because the loop iterates k times, and the operations inside the loop take constant time.

Comment

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