Sure. The Java code describes the computation you want done, just like the math-like definition of Triangle.

Here is a complete program for testing this method. The value for N is hard-coded. You might wish to improve the program so that N is entered by the user.

class TriangleCalc { int Triangle( int N ) { if ( N == 1 ) return 1; else return N + Triangle( N-1 ); } } class TriangleTester { public static void main ( String[] args) { TriangleCalc tri = new TriangleCalc(); int result = tri.Triangle( 4 ); System.out.println("Triangle(4) is " + result ); } }

Here is the output of the program:

C:\>java TriangleTester Triangle(4) is 10 C:\>

Of course, it would be worth while at this point to copy this program to a file and run it.