Java:

public class TriangularNumbers {
  public int get(int no) {
	if (no == 1) {
	  return no;
	} else {
	  return no + get(no - 1);
	}
  }
  
  public static void main(String[] args) {
	TriangularNumbers triangularNumbers = new TriangularNumbers();
	System.out.println(triangularNumbers.get(4));
  }
}

Python:

def triangular_no(no)  :
	if no == 1:
		return no
	return no + triangular_no(no - 1)

print triangular_no(4)
Advertisements