> For the complete documentation index, see [llms.txt](https://educacion.gitbook.io/programacion/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://educacion.gitbook.io/programacion/exercicios-java/ud3-solucions/recursividade.md).

# Recursividade

```java
public static String invertir(String palabra) {
    // CASO BASE
    if (palabra.length() <= 1)
        return palabra;

    // CASO RECURSIVO
    return invertir(palabra.substring(1)) + palabra.charAt(0);
}
```

```java
public static int suma(int n) throws Exception{
    // Si n es negativo, podría lanzar excepción
    if (n <= 0) throw new Exception("Numero negativo no soportado"
    if (n == 1) return 1;
    return n + suma(n - 1);
}
```

```java
public static int contaCifras(int numero, int cifra) {
    
    
    // CASO BASE: cando xa non quedan díxitos
    if (numero == 0) {
        return 0;
    }
    
    // Comprobamos o último díxito
    int ultima = numero % 10;

    // CASO RECURSIVO: sumamos 1 se coincide e seguimos co resto
    if (ultima == cifra)
        return 1 + contaCifras(numero / 10, cifra);
    else
        return contaCifras(numero / 10, cifra);
}
```

```java
public static int potencia(int base, int exp) throws Exception {
    // CASO BASE 1: exponente 0 (cualquier número^0 = 1)
    if (exp == 0) {
        return 1;
    }
    
    // CASO BASE 2: exponente 1 (cualquier número^1 = sí mismo)
    if (exp == 1) {
        return base;
    }
    
    // CASO BASE 3: exponente negativo (no manejado, podríamos lanzar excepción)
    if (exp < 0) {
        // Podríamos hacer: return 1 / potencia(base, -exp) pero sería double
        throw new Exception("Exponente negativo no soportado");
    }
    
    // CASO RECURSIVO: base^exp = base * base^(exp-1)
    return base * potencia(base, exp - 1);
}
```

```java
public static boolean palindromo(String palabra){
        int len = palabra.length();
        if (len==0 || len ==1)
            return true;
        else if (palabra.charAt(0)!=palabra.charAt(len-1)){
            return false;
        } else {
            return palindromo(palabra.substring(1,len-1));
        }
    }
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://educacion.gitbook.io/programacion/exercicios-java/ud3-solucions/recursividade.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
