Posted on by Kalkicode
Code Pattern

Natural numbers summation pattern

The natural numbers summation pattern is a sequence of numbers where each row represents the sum of natural numbers up to a certain height. The objective of this pattern is to display the sum of numbers in each row, starting from 1 and incrementing by 1 until the given height is reached. This article provides an explanation of the problem, a suitable example, the algorithm and pseudocode, and an explanation of the resultant output with the time complexity of the code.

Problem Explanation

The problem is to print a pattern that displays the sum of natural numbers in each row up to a given height. The height determines the number of rows to be printed. Each row shows the sum of the natural numbers from 1 to the current row number. For example, if the height is 5, the pattern will display:

1 = 1
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15

Example and Explanation

Let's take the example of a height of 5 to better understand the problem. In the first row, only the number 1 is present, so the sum is 1. In the second row, the numbers 1 and 2 are present, and their sum is 3. In the third row, the numbers 1, 2, and 3 are present, resulting in a sum of 6. This pattern continues until the last row, where all natural numbers up to the height are present, and their sum is displayed.

Algorithm and Pseudocode

The algorithm to solve this problem is straightforward. We iterate through each row, from 1 to the given height. Within each row, we iterate from 1 to the current row number, calculating the sum of the numbers. The pseudocode for the algorithm is as follows:


function printPattern(height):
    for i from 1 to height:
        sum = 0
        for j from 1 to i:
            sum += j
            if j == 1:
                print j
            else:
                print " + " + j
        print " = " + sum

Code Solution

//  C program for
//  Natural numbers summation pattern
#include <stdio.h>

void printPattern(int height)
{
	// Display given height
	printf("\n Height : %d  \n", height);
	int sum = 0;
	for (int i = 1; i <= height; ++i)
	{
		for (int j = 0; j < i; ++j)
		{
			sum += j + 1;
			// Print element value
			if (j == 0)
			{
				printf(" %d", j + 1);
			}
			else
			{
				printf(" + %d", j + 1);
			}
		}
		// Print sum
		printf(" = %d\n", sum);
		sum = 0;
	}
}
int main(int argc, char
	const *argv[])
{
	// Test
	printPattern(5);
	printPattern(9);
	printPattern(4);
	return 0;
}

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
/*
    Java Program
    Natural numbers summation pattern
*/
public class Pattern
{
	public void printPattern(int height)
	{
		// Display given height
		System.out.println("\n Height : " + height);
		int sum = 0;
		for (int i = 1; i <= height; ++i)
		{
			for (int j = 0; j < i; ++j)
			{
				sum += j + 1;
				// Print element value
				if (j == 0)
				{
					System.out.print(" " + (j + 1));
				}
				else
				{
					System.out.print(" + " + (j + 1));
				}
			}
			// Print sum
			System.out.println(" = " + sum);
			sum = 0;
		}
	}
	public static void main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(4);
	}
}

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
// Include header file
#include <iostream>
using namespace std;
/*
    C++ Program
    Natural numbers summation pattern
*/
class Pattern
{
	public: void printPattern(int height)
	{
		// Display given height
		cout << "\n Height : " << height << endl;
		int sum = 0;
		for (int i = 1; i <= height; ++i)
		{
			for (int j = 0; j < i; ++j)
			{
				sum += j + 1;
				// Print element value
				if (j == 0)
				{
					cout << " " << (j + 1);
				}
				else
				{
					cout << " + " << (j + 1);
				}
			}
			// Print sum
			cout << " = " << sum << endl;
			sum = 0;
		}
	}
};
int main()
{
	Pattern *task = new Pattern();
	// Test
	task->printPattern(5);
	task->printPattern(9);
	task->printPattern(4);
	return 0;
}

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
// Include namespace system
using System;
/*
    Csharp Program
    Natural numbers summation pattern
*/
public class Pattern
{
	public void printPattern(int height)
	{
		// Display given height
		Console.WriteLine("\n Height : " + height);
		int sum = 0;
		for (int i = 1; i <= height; ++i)
		{
			for (int j = 0; j < i; ++j)
			{
				sum += j + 1;
				// Print element value
				if (j == 0)
				{
					Console.Write(" " + (j + 1));
				}
				else
				{
					Console.Write(" + " + (j + 1));
				}
			}
			// Print sum
			Console.WriteLine(" = " + sum);
			sum = 0;
		}
	}
	public static void Main(String[] args)
	{
		Pattern task = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(4);
	}
}

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
package main
import "fmt"
/*
    Go Program
    Natural numbers summation pattern
*/
type Pattern struct {}
func getPattern() * Pattern {
	var me *Pattern = &Pattern {}
	return me
}
func(this Pattern) printPattern(height int) {
	// Display given height
	fmt.Println("\n Height : ", height)
	var sum int = 0
	for i := 1 ; i <= height ; i++ {
		for j := 0 ; j < i ; j++ {
			sum += j + 1
			// Print element value
			if j == 0 {
				fmt.Print(" ", (j + 1))
			} else {
				fmt.Print(" + ", (j + 1))
			}
		}
		// Print sum
		fmt.Println(" = ", sum)
		sum = 0
	}
}
func main() {
	var task * Pattern = getPattern()
	// Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(4)
}

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
<?php
/*
    Php Program
    Natural numbers summation pattern
*/
class Pattern
{
	public	function printPattern($height)
	{
		// Display given height
		echo("\n Height : ".$height."\n");
		$sum = 0;
		for ($i = 1; $i <= $height; ++$i)
		{
			for ($j = 0; $j < $i; ++$j)
			{
				$sum += $j + 1;
				// Print element value
				if ($j == 0)
				{
					echo(" ".($j + 1));
				}
				else
				{
					echo(" + ".($j + 1));
				}
			}
			// Print sum
			echo(" = ".$sum."\n");
			$sum = 0;
		}
	}
}

function main()
{
	$task = new Pattern();
	// Test
	$task->printPattern(5);
	$task->printPattern(9);
	$task->printPattern(4);
}
main();

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
/*
    Node JS Program
    Natural numbers summation pattern
*/
class Pattern
{
	printPattern(height)
	{
		// Display given height
		console.log("\n Height : " + height);
		var sum = 0;
		for (var i = 1; i <= height; ++i)
		{
			for (var j = 0; j < i; ++j)
			{
				sum += j + 1;
				// Print element value
				if (j == 0)
				{
					process.stdout.write(" " + (j + 1));
				}
				else
				{
					process.stdout.write(" + " + (j + 1));
				}
			}
			// Print sum
			console.log(" = " + sum);
			sum = 0;
		}
	}
}

function main()
{
	var task = new Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(4);
}
main();

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
#    Python 3 Program
#    Natural numbers summation pattern
class Pattern :
	def printPattern(self, height) :
		#  Display given height
		print("\n Height : ", height)
		sum = 0
		i = 1
		while (i <= height) :
			j = 0
			while (j < i) :
				sum += j + 1
				#  Print element value
				if (j == 0) :
					print(" ", (j + 1), end = "",sep="")
				else :
					print(" + ", (j + 1), end = "",sep="")
				
				j += 1
			
			#  Print sum
			print(" = ", sum)
			sum = 0
			i += 1
		
	

def main() :
	task = Pattern()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(4)

if __name__ == "__main__": main()

Output

 Height :  5
 1 =  1
 1 + 2 =  3
 1 + 2 + 3 =  6
 1 + 2 + 3 + 4 =  10
 1 + 2 + 3 + 4 + 5 =  15

 Height :  9
 1 =  1
 1 + 2 =  3
 1 + 2 + 3 =  6
 1 + 2 + 3 + 4 =  10
 1 + 2 + 3 + 4 + 5 =  15
 1 + 2 + 3 + 4 + 5 + 6 =  21
 1 + 2 + 3 + 4 + 5 + 6 + 7 =  28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 =  36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 =  45

 Height :  4
 1 =  1
 1 + 2 =  3
 1 + 2 + 3 =  6
 1 + 2 + 3 + 4 =  10
#    Ruby Program
#    Natural numbers summation pattern
class Pattern 
	def printPattern(height) 
		#  Display given height
		print("\n Height : ", height, "\n")
		sum = 0
		i = 1
		while (i <= height) 
			j = 0
			while (j < i) 
				sum += j + 1
				#  Print element value
				if (j == 0) 
					print(" ", (j + 1))
				else
 
					print(" + ", (j + 1))
				end

				j += 1
			end

			#  Print sum
			print(" = ", sum, "\n")
			sum = 0
			i += 1
		end

	end

end

def main() 
	task = Pattern.new()
	#  Test
	task.printPattern(5)
	task.printPattern(9)
	task.printPattern(4)
end

main()

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
/*
    Scala Program
    Natural numbers summation pattern
*/
class Pattern()
{
	def printPattern(height: Int): Unit = {
		// Display given height
		println("\n Height : " + height);
		var sum: Int = 0;
		var i: Int = 1;
		while (i <= height)
		{
			var j: Int = 0;
			while (j < i)
			{
				sum += j + 1;
				// Print element value
				if (j == 0)
				{
					print(" " + (j + 1));
				}
				else
				{
					print(" + " + (j + 1));
				}
				j += 1;
			}
			// Print sum
			println(" = " + sum);
			sum = 0;
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Pattern = new Pattern();
		// Test
		task.printPattern(5);
		task.printPattern(9);
		task.printPattern(4);
	}
}

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
/*
    Swift 4 Program
    Natural numbers summation pattern
*/
class Pattern
{
	func printPattern(_ height: Int)
	{
		// Display given height
		print("\n Height : ", height);
		var sum: Int = 0;
		var i: Int = 1;
		while (i <= height)
		{
			var j: Int = 0;
			while (j < i)
			{
				sum += j + 1;
				// Print element value
				if (j == 0)
				{
					print(" ", (j + 1), terminator: "");
				}
				else
				{
					print(" + ", (j + 1),separator:"", terminator: "");
				}
				j += 1;
			}
			// Print sum
			print(" = ", sum);
			sum = 0;
			i += 1;
		}
	}
}
func main()
{
	let task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(4);
}
main();

Output

 Height :  5
  1 =  1
  1 + 2 =  3
  1 + 2 + 3 =  6
  1 + 2 + 3 + 4 =  10
  1 + 2 + 3 + 4 + 5 =  15

 Height :  9
  1 =  1
  1 + 2 =  3
  1 + 2 + 3 =  6
  1 + 2 + 3 + 4 =  10
  1 + 2 + 3 + 4 + 5 =  15
  1 + 2 + 3 + 4 + 5 + 6 =  21
  1 + 2 + 3 + 4 + 5 + 6 + 7 =  28
  1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 =  36
  1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 =  45

 Height :  4
  1 =  1
  1 + 2 =  3
  1 + 2 + 3 =  6
  1 + 2 + 3 + 4 =  10
/*
    Kotlin Program
    Natural numbers summation pattern
*/
class Pattern
{
	fun printPattern(height: Int): Unit
	{
		// Display given height
		println("\n Height : " + height);
		var sum: Int = 0;
		var i: Int = 1;
		while (i <= height)
		{
			var j: Int = 0;
			while (j < i)
			{
				sum += j + 1;
				// Print element value
				if (j == 0)
				{
					print(" " + (j + 1));
				}
				else
				{
					print(" + " + (j + 1));
				}
				j += 1;
			}
			// Print sum
			println(" = " + sum);
			sum = 0;
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Pattern = Pattern();
	// Test
	task.printPattern(5);
	task.printPattern(9);
	task.printPattern(4);
}

Output

 Height : 5
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15

 Height : 9
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10
 1 + 2 + 3 + 4 + 5 = 15
 1 + 2 + 3 + 4 + 5 + 6 = 21
 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45

 Height : 4
 1 = 1
 1 + 2 = 3
 1 + 2 + 3 = 6
 1 + 2 + 3 + 4 = 10

Explanation of Resultant Output

The resultant output of the provided code is printed for three different heights: 5, 9, and 4. For each height, the code correctly prints the pattern as explained earlier. It starts with the height value, followed by each row representing the sum of natural numbers up to that row. The sum is calculated using nested loops. The inner loop calculates the sum, and the outer loop controls the number of rows. At the end of each row, the sum is printed, and the variable is reset to 0 for the next row. The output demonstrates the correct execution of the algorithm and the accuracy of the code implementation.

Time Complexity

The time complexity of the provided code is O(n^2), where n is the given height. This is because there are two nested loops: one loop runs for the given height, and the other loop runs up to the current row number. As a result, the time complexity increases with the square of the height. It is important to note that the code complexity is efficient for small values of height, but it may become slower for larger values due to the quadratic nature of the algorithm.

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