- Percorra uma lista de Strings e exiba seu conteúdo;
- Exiba o Enésimo (N) número da sequência de Fibonacci;
- Calcule o fatorial de um número;
Created
September 13, 2024 14:12
-
-
Save SpinnerZ/90fb5b706dead1d5caaba62222ff2e3e to your computer and use it in GitHub Desktop.
Lógica de programação com Java: Recursividade
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.example; | |
| public class Main { | |
| public static void main(String[] args) { | |
| System.out.println(); | |
| // printCharSequence((char) 32, (char) 255); | |
| printCharSequence(' ', 'ÿ'); | |
| System.out.println("\n\n"); | |
| decrescentPrintCharSequence(' ', 'ÿ'); | |
| // printCharSequenceNonRecursive((char) 32, (char) 255); | |
| System.out.println("\n\n"); | |
| System.out.println(sum(100)); | |
| } | |
| public static void printCharSequenceNonRecursive(char actual, char limit) { | |
| for (; actual <= limit; actual++) { | |
| System.out.print(actual + " "); | |
| } | |
| } | |
| public static void printCharSequence(char actual, char limit) { | |
| if (actual <= limit) { | |
| System.out.print(actual + " "); | |
| printCharSequence(++actual, limit); | |
| } | |
| } | |
| public static void decrescentPrintCharSequence(char actual, char limit) { | |
| if (actual <= limit) { | |
| decrescentPrintCharSequence(++actual, limit); | |
| } | |
| System.out.print(actual + " "); | |
| } | |
| /* | |
| Chamada 1 -> Imprime | |
| Chamada 2 -> Imprime | |
| Chamada 3 -> Imprime | |
| Chamada N -> Imprime | |
| */ | |
| // Programa que faça a soma de 0 até N | |
| private static int sum(int n) { | |
| if (n > 0) { | |
| return n + sum(n - 1); | |
| } | |
| return n; | |
| } | |
| /* | |
| 1ª n = 3 | |
| 2ª n = 2 | |
| 3ª n = 1 | |
| 4ª n = 0 -> retorna 0 | |
| 3ª n = 1 -> retorna 1 + 0 = 1 | |
| 2ª n = 2 -> retorna 2 + 1 = 3 | |
| 1ª n = 3 -> retorna 3 + 3 = 6 | |
| */ | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment