- package text;
- import java.util.Scanner;
- public class JPA04 {
- static Scanner keyboard = new Scanner(System.in);
- public static void main(String args[]) {
-
- System.out.print("Input the number n: ");
- int num=keyboard.nextInt();
- System.out.println("Ans: "+f(num));
- }
-
- static int f(int n)
- {
- if(n==1)
- {
- return 2;
- }
- else
- return f(n-1)+2*n;
- }
-
- }
複製代碼 |