-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
31 lines (30 loc) · 1017 Bytes
/
Test.java
File metadata and controls
31 lines (30 loc) · 1017 Bytes
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
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test{
// A SIMPLE RECURSIVE METHOD TO GET THE FACTORIAL OF A NUMBER
public static int getTheSum(int n){
int sum = 0;
if(n > 0) sum += n + getTheSum(n - 1);
return sum;
}
// A SIMPLE RECURSIVE METHOD TO GET THE FACTORIAL OF A NUMBER
public static long getFactorialN(int n){
long factorial = 1;
if(n > 0) factorial *= n * getFactorialN(n - 1);
return factorial;
}
public static void main(String[] args){
//System.out output stream writer
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(System.out)
);
try{
writer.write("The sum is: " + getTheSum(5) + "\n");
writer.write("The factorial is: " + getFactorialN(5) + "\n");
writer.flush();
}catch(IOException e){
System.out.print(e.getMessage());
}
}
}