﻿ C# - Find the nth tetrahedral number from an integer value

# 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:
NTetrahedral
Number
11
24
310
420
535
656

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

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿