> 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/ud2-solucions/funcions.md).

# Funcións

1. Primeiro Múltiplo

```java
public class PrimeiroMultiplo {

    // Función que devolve o primeiro múltiplo distinto do número introducido
    static int buscarPrimeiroMultiplo(int num) {
        int multiplo = -1; // valor por defecto se non se atopa ningún

        for (int i = 1; i <= 100; i++) {
            if (i % num == 0 && i != num) {  // comprobamos se é múltiplo e distinto
                multiplo = i;
                break; // saída anticipada: atopamos o primeiro, xa podemos parar
            }
        }

        return multiplo;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int numero, resultado;
        System.out.print("Introduce un número do 1 ao 10: ");
        
        numero = sc.nextInt();
        resultado = buscarPrimeiroMultiplo(numero);

        if (resultado != -1) {
            System.out.println("O primeiro múltiplo de " + numero + " distinto de el mesmo é: " + resultado);
        } else {
            System.out.println("Non se atopou ningún múltiplo nos 100 primeiros números.");
        }
    
        sc.close();
    }
}
```

2. Multiplos de 7 e impares multiplos de 5

```java
public class NumerosFiltrados {

    // Procedemento: mostra os n primeiros números aplicando as condicións
    static void mostrarNumeros(int n) {
        for (int i = 1; i <= n; i++) {

            // Excluír múltiplos de 7
            if (i % 7 == 0) continue;

            // Excluír impares que tamén son múltiplos de 5
            if (i % 2 != 0 && i % 5 == 0) continue;

            // Mostrar o número que pasa os filtros
            System.out.println(i);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Introduce o valor de n: ");
        int n = sc.nextInt();

        System.out.println("Mostrando os " + n + " primeiros números (sen múltiplos de 7 nin impares múltiplos de 5):");
        mostrarNumeros(n);  // chamada ao procedemento

        sc.close();
    }
}
```

3. Suma posiciones impares

```java
static String sumaPosiciones(int numero) {
    if (numero < 0) numero = -numero;
    
    int sumaPares = 0;
    int sumaImpares = 0;
    int posicion = 0;
    
    while (numero > 0) {
        int digito = numero % 10;
        
        if (posicion % 2 == 0) {
            sumaPares += digito;
        } else {
            sumaImpares += digito;
        }
        
        numero /= 10;
        posicion++;
    }
    
    return "Suma posiciones pares: " + sumaPares + ", Suma posiciones impares: " + sumaImpares;
}
```

4. Maximo común divisor

```java
public class MaximoComunDivisor {

    // Función que calcula o MCD probando todos os divisores posibles
    static int calcularMCD(int a, int b) {
        int menor;  // gardará o menor dos dous números
        int mcd = 1; // acumulador do máximo divisor común

        menor = (int) Math.min(a,b);

        // Recorremos dende 1 ata o menor número
        for (int i = 1; i <= menor; i++) {
            // Se i divide a ambos, actualizamos o MCD
            if (a % i == 0 && b % i == 0) {
                mcd = i;
            }
        }

        return mcd;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Introduce o primeiro número: ");
        int num1 = sc.nextInt();

        System.out.print("Introduce o segundo número: ");
        int num2 = sc.nextInt();

        int mcd = calcularMCD(num1, num2);

        System.out.println("O máximo común divisor de " + num1 + " e " + num2 + " é: " + mcd);

        sc.close();
    }
}
```

5. posicionDigito

```java
static int posicionDigito(int numero, int digitoBuscado) {
    // Ej: posicionDigito(1234, 3) → 2 (desde derecha)
    // Ej: posicionDigito(1234, 5) → -1
    int posicion = 0;
    while (numero > 0) {
        if (numero % 10 == digitoBuscado) {
            return posicion;
        }
        posicion++;
        numero /= 10;
    }
    return -1;
}
```

6\. digitosUnicos

```java
static boolean todosDigitosUnicos(int numero) {
    // Ej: todosDigitosUnicos(123) → true
    // Ej: todosDigitosUnicos(122) → false
    int n = numero;
    
    while (n > 0) {
        int digitoActual = n % 10;
        int temp = n / 10;
        
        while (temp > 0) {
            if (temp % 10 == digitoActual) {
                return false; // Encontrado repetido
            }
            temp /= 10;
        }
        n /= 10;
    }
    return true;
}
```

7\.

```java
static int inverterNumero(int n) throws Exception {
    if (n <= 0) {
        throw new Exception("O número debe ser positivo");
    }

    int invertido = 0;

    while (n > 0) {
        int digito = n % 10;
        invertido = invertido * 10 + digito;
        n = n / 10;
    }

    return invertido;
}
```


---

# 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/ud2-solucions/funcions.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.
