Formula for Triangle

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.

Answer:

Yes.

Formula for Triangle

Triangle is so easy that you don't need to use either iteration or recursion for it. You might recognize Triangle(N) as the sum of the series

1 + 2 + 3 + ... + (N-1) + N = (N(N+1)) / 2

For example

1 + 2 + 3 + 4  = (4(5)) / 2 = 10

Of course a Java method that implements this formula is simple. And that is how you would do it in a practical situation. But the goal of this chapter was to show recursion with Java and triangle numbers work well for that.

QUESTION 20:

Are you ready for another example?