일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- 자바암호
- GIT
- Spring boot
- 유닉스
- write by chatGPT
- python
- 파이썬
- kotlin
- 뉴턴역학
- 소프트웨어공학
- 인프라
- Java
- oracle
- 역학
- 고전역학
- lombok
- JVM
- 웹 크롤링
- Database
- flet
- 코틀린
- 시스템
- GPT-4's answer
- chatGPT's answer
- NIO
- 리눅스
- 자바네트워크
- android
- 자바
- write by GPT-4
- Today
- Total
Akashic Records
Top 5 Java recursion examples 본문
- Print a series of numbers with recursive Java methods
- Sum a series of numbers with Java recursion
- Calculate a factorial in Java with recursion
- Print the Fibonacci series with Java and recursion
- A recursive Java palindrome checker
1. A simple Java recursion example
A simple program is always the best place to start when you learn a new concept. This first Java recursion example simply prints out the digits from one to the selected number in reverse order.
package com.mcnz.recursion;
public class VerySimpleRecursionExample {
public static void main(String[] args) {
callMyself(9);
}
/* The recursive Java method */
public static void callMyself(long i) {
if (i < 0) {
return;
}
System.out.print(i);
i = i - 1;
callMyself(i);
}
}
2. Sum numbers with recursive Java example
The recursive Java logic is as follows. Start with a number and then add that number to one less than itself. Repeat that logic until you hit zero. Once zero is encountered, the total sum of all numbers from the starting number down to zero has been calculated.
package com.mcnz.recursion;
public class RecursiveSumOfAllNumbers {
public static void main(String[] args) {
long sumOfAllNumbers = sumOfAllNumbers(9);
System.out.println(sumOfAllNumbers);
}
/* A recursive Java example to sum all natural numbers up to a given long. */
public static long sumOfAllNumbers(long number) {
/* Stop the recursive Java program at zero */
if (number != 0) {
return number + sumOfAllNumbers(number - 1);
} else {
return number;
}
}
}
3. Recursive Java factorial program
package com.mcnz.recursion;
public class RecursiveJavaFactorialProgram {
public static void main(String args[]) {
long nFactoriral = factorialProgram(9);
System.out.println(nFactoriral);
}
/* Java factorial program with recursion. */
public static long factorialProgram(long n) {
if (n <= 1) {
return 1;
} else {
return n * factorialProgram(n - 1);
}
}
}
4. Recursive Fibonacci series in Java example
The most common assignment to task young developers learning about recursion is to calculate the Fibonacci series in both iterative and recursive Java programs.
package com.mcnz.recursion;
public class RecursiveFibonnaciJavaProgram {
public static void main (String args[]) {
for(long i=1; i<=9; i++){
System.out.print(fibonacci(i) +" ");
}
System.out.println();
}
/* A recursive Fibonnaci sequence in Java program */
public static long fibonacci(long number) {
if (number == 1 || number == 2) {
return 1;
}
return fibonacci(number - 1) + fibonacci(number - 2);
}
}
5. Recursive Java palindrome program
All of the Java recursion examples so far deal with numbers. The recursive Java palindrome checker program deals with strings, namely to see if a string is spelled the exact same way when the letters in the word are reversed.
package com.mcnz.recursion;
public class JavaPalindromeCheckProgram {
public static void main(String[] args) {
boolean flag = palindromeCheck("sitonapanotis");
System.out.println(flag);
flag = palindromeCheck("nine");
System.out.println(flag);
flag = palindromeCheck("amanaplanacanalpanama");
System.out.println(flag);
}
/* Recursive Java example to check for palindromes */
public static boolean palindromeCheck(String s){
if(s.length() == 0 || s.length() == 1) {
return true;
}
if(s.charAt(0) == s.charAt(s.length()-1)) {
return palindromeCheck(s.substring(1, s.length()-1));
}
return false;
}
}
'Library' 카테고리의 다른 글
Xfce, Xubuntu Install and Remove (0) | 2021.02.16 |
---|---|
@Secured, @PreAuthorize, @PostAuthorize (0) | 2021.01.27 |
Docker Compose 설치 (0) | 2021.01.07 |
Lombok features - @With (0) | 2020.12.21 |
Lombok features - @Synchronized (0) | 2020.12.21 |