-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.java
More file actions
66 lines (62 loc) · 1.61 KB
/
App.java
File metadata and controls
66 lines (62 loc) · 1.61 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.example;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
/**
* Calculates the sum of integers from 1 to n.
*
* @param n the upper limit of the range (inclusive)
* @return the sum of integers from 1 to n, or 0 if n is less than or equal to 0
*/
public static int sum_to_n(int n) {
if (n <= 0) {
return 0;
}
int total = 0;
for (int i = 1; i <= n; i++) {
total += i;
}
return total;
}
/**
* Sorts an array of integers using the bubble sort algorithm.
*
* @param array the array of integers to be sorted
* @return the sorted array
*/
public static int[] bubble_sort(int[] array) {
int n = array.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}
/**
* Calculates the nth Fibonacci number using recursion.
*
* @param n the position in the Fibonacci sequence (1-based index)
* @return the nth Fibonacci number, or 0 if n is less than or equal to 0
*/
public static int fibonacci(int n) {
if (n <= 0) {
return 0;
}
if (n == 1) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}