Natural numbers summation pattern

Here given code implementation process.

//  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


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