HomeContact

How to do Recursions in Java

Published in Java
May 11, 2023
1 min read

Recursive calls call back the magnetic function in the function, and if there is no stopping condition, it is repeated indefinitely, so be careful.

public static void main(String[] args) {
int result = sum(1, 4);
System.out.println(result);
}
public static int sum(int start, int end) {
if (end > start) { // Stop conditions preventing infinite recursive repetition
return end + sum(start, end - 1);
} else {
return end;
}
}

Tags

#Java#java.lang

Share


Previous Article
How to use Switch statements in Java

Topics

Java
Other
Server

Related Posts

How to use BigInteger in Java - 2
May 31, 2023
1 min