-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSumNaturalNum.java
More file actions
40 lines (34 loc) · 1.63 KB
/
Copy pathSumNaturalNum.java
File metadata and controls
40 lines (34 loc) · 1.63 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
package SumNaturalNum;
import java.util.Scanner; // Scanner class is required to get input from user
public class SumNaturalNum {
static int n = 0; // n number of integers, initialised to zero for now.
static int sumValue = 0; // sum of the first n integers, initialised to zero for now.
public static void main(String[] args) {
// if user provided commandline-arguments, then
if (args.length != 0) {
if (args.length == 1)
n = Integer.parseInt(args[0]);
else
System.out.println("[!] Available arguments: [n, the first 'n' integers upto which sum is to be found]");
} // else get input from user
else {
Scanner scanner = new Scanner(System.in);
System.out.print("[<-] Enter the number of terms: ");
n = scanner.nextInt();
scanner.close();
}
sumValue = findSumNaturalNumbers(n); // getting the result from the recursive method
if (sumValue == -1)
System.out.println("[->] Seems like you entered a non-natural number, cannot find the sum !");
else
System.out.println("[->] Sum of the first " + n + " integers is " + sumValue + ".");
}
// a recursive method to find the sum of first n-natural numbers.
public static int findSumNaturalNumbers(int number) {
if (number < 0) // if the number is a negative integer then return -1
return -1;
if (number == 0 || number == 1) // if the number is 0 or 1 then return 0 or 1 respectively.
return number;
return number + findSumNaturalNumbers(number - 1);
}
}