We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 823c1fa + 7c69831 commit c2ca3caCopy full SHA for c2ca3ca
DailyCodingProblem/src/Mathematics/SumOfNaturalNumber.java
@@ -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