Skip to content

Instantly share code, notes, and snippets.

@peter279k
Created May 24, 2017 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peter279k/ad4970b187058019d683d1cbb6e3ac40 to your computer and use it in GitHub Desktop.
Save peter279k/ad4970b187058019d683d1cbb6e3ac40 to your computer and use it in GitHub Desktop.
TQC+ JAVA
import java.util.Scanner;
public class JPA03 {
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: " + sum2(num));
}
public static int sum2(int num) {
if(num < 1) {
return 0;
}
if(num == 1) {
return 2;
}
return sum2(num - 1) + 2 * num;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment