C#: Get the nth tetrahedral number from a given integer(n) value
C# Sharp Math: Exercise-21 with Solution
Write a C# Sharp program to get the nth tetrahedral number from a given integer(n) value.
A tetrahedral number, or triangular pyramidal number, is a figurate number that represents a pyramid with a triangular base and three sides, called a tetrahedron. The formula for the nth tetrahedral number is represented by the 3rd rising factorial of n divided by the factorial of 3:
Example of tetrahedral numbers:
N | Tetrahedral Number |
---|---|
1 | 1 |
2 | 4 |
3 | 10 |
4 | 20 |
5 | 35 |
6 | 56 |
Sample Solution:
C# Sharp Code:
using System;
using System.Linq;
namespace exercises
{
class Program
{
static void Main(string[] args)
{
int n = 1;
Console.WriteLine("\nOriginal Number:" + n);
Console.WriteLine("Tetrahedral number:" + test(n));
n = 2;
Console.WriteLine("\nOriginal Number:" + n);
Console.WriteLine("Tetrahedral number:" + test(n));
n = 6;
Console.WriteLine("\nOriginal Number:" + n);
Console.WriteLine("Tetrahedral number:" + test(n));
}
public static int test(int n)
{
return n * (n + 1) * (n + 2) / 6;
}
}
}
Sample Output:
Original Number:1 Tetrahedral number:1 Original Number:2 Tetrahedral number:4 Original Number:6 Tetrahedral number:56
Flowchart:
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to get the Least Common Multiple (LCM) of more than two numbers. Take the numbers from a given array of positive integers. Next: Write a C# Sharp program to sort a given positive number in descending/ascending order.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/csharp-exercises/math/csharp-math-exercise-21.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics