-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterativeSum.java
More file actions
38 lines (28 loc) · 782 Bytes
/
IterativeSum.java
File metadata and controls
38 lines (28 loc) · 782 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
32
33
34
35
36
37
38
import java.util.Scanner; // for input objects
// DONE
public class IterativeSum
{
static int calculation(int a)
{
int sum = 0; // this will be returned
for (int i = 1; i <= a; i++)
{
sum += i;
}
return sum;
}
public static void main(String[] args)
{
Scanner obj = new Scanner(System.in);
int n; // limit of natrual numbers to be entered
do // PROMPT WITH VALIDATION
{
System.out.print(" Enter Number for Sum Limit : ");
n = obj.nextInt();
} while (n < 1);
// OUTPUT With returning value
System.out.println(" Sum from 1 to " + n + " = " + calculation(n));
// closing Scanner object
obj.close();
}
}