w3resource

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:

C# Sharp Exercises: Get the nth tetrahedral number from a given integer(n) value.
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:

Flowchart: C# Sharp Exercises - Get the nth tetrahedral number from a given integer(n) value.

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.



Follow us on Facebook and Twitter for latest update.