Recursively gets the sum of squares of numbers starting from num to 1 - Java java.lang

Java examples for java.lang:Math Number

Description

Recursively gets the sum of squares of numbers starting from num to 1

Demo Code


//package com.java2s;

public class Main {
    /**// w ww.j  a  va 2 s.c  o  m
     * Recursively gets the sum of squares of numbers starting from num to 1
     *
     * @param num Number to start at
     * @return The sum of the squares
     */
    public static long sumOfSquares(long num) {
        if (num == 1 || num == 0) {
            return num;
        } else {
            return (num * num) + sumOfSquares(num - 1);
        }
    }
}

Related Tutorials