import java.util.Scanner; /** * Computs triangular numbers in a completely recursive way. **/ public class Triangular { /** * Use recursion to add numers 1..num **/ public int f(int num) { if (num==0) return 0; return num + f(num-1); } /** * Use recursion to do triangular numbers **/ public int triangular(int num) { if (num==0) return 0; return f(num) + triangular(num-1); } /** * Gets input from user **/ public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Triangular tri = new Triangular(); while (true) { System.out.println("Integer:"); int xx = scanner.nextInt(); if (xx==0) { System.out.println("Goofbye"); return; } System.out.println("Trangular number " + xx + " is " + tri.triangular(xx)); } } }