Skip to content

Commit dac827b

Browse files
Merge pull request #398 from PRITI1999/master
Sum of N Natural Numbers
2 parents 00ce530 + bc838e0 commit dac827b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
@author : Priti Chattopadhyay
3+
*/
4+
5+
//importing necessary packages
6+
import java.util.*;
7+
8+
public class sum_of_natural_numbers{
9+
10+
//one line to find sum of N natural numbers
11+
int findSum(int N){ return (N*(N + 1))/2;}
12+
13+
//Driver code to test the function
14+
public static void main(String args[]){
15+
Scanner sc = new Scanner(System.in);
16+
sum_of_natural_numbers obj = new sum_of_natural_numbers();
17+
int N;
18+
N = sc.nextInt();
19+
System.out.println(obj.findSum(N));
20+
}
21+
}

0 commit comments

Comments
 (0)