Skip to content

Commit c2ca3ca

Browse files
Merge pull request #13 from princeghoshc7/SumOfNaturalNumber
Sum
2 parents 823c1fa + 7c69831 commit c2ca3ca

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package Mathematics;
2+
3+
import java.util.Scanner;
4+
5+
public class SumOfNaturalNumber {
6+
7+
public static int calculateSum(int num) {
8+
9+
return (num*(num+1)/2);
10+
}
11+
12+
public static void main(String[] args) {
13+
// TODO Auto-generated method stub
14+
Scanner sc=new Scanner(System.in);
15+
16+
System.out.print("Enter the positive interger number : " );
17+
int n=sc.nextInt();
18+
19+
if(n<0) {
20+
System.out.println("Invalide Integer ! ");
21+
}
22+
else {
23+
24+
int sum=calculateSum(n);
25+
System.out.println("Sum of first " + n + " natural number is : " + sum);
26+
}
27+
28+
29+
}
30+
31+
}

0 commit comments

Comments
 (0)