Skip to content

Commit 131c9b5

Browse files
committed
Simple Adding
1 parent 155a3ee commit 131c9b5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/easy/SimpleAdding.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package easy;
2+
3+
/**
4+
* Have the function SimpleAdding(num) add up all the numbers from 1 to num.
5+
* For example: if the input is 4 then your program
6+
* should return 10 because 1 + 2 + 3 + 4 = 10.
7+
* For the test cases, the parameter num will be any number from 1 to 1000.
8+
*/
9+
public class SimpleAdding {
10+
11+
/**
12+
* Simple Adding function.
13+
*
14+
* @param num input number
15+
* @return the sum of numbers
16+
*/
17+
private static int simpleAdding(int num) {
18+
return num * (num + 1) / 2;
19+
}
20+
21+
/**
22+
* Entry point.
23+
*
24+
* @param args command line arguments
25+
*/
26+
public static void main(String[] args) {
27+
var result1 = simpleAdding(100);
28+
System.out.println(result1);
29+
var result2 = simpleAdding(8);
30+
System.out.println(result2);
31+
}
32+
33+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy