Skip to content

Sum #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 2, 2025
Merged

Sum #13

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions DailyCodingProblem/src/Mathematics/SumOfNaturalNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package Mathematics;

import java.util.Scanner;

public class SumOfNaturalNumber {

public static int calculateSum(int num) {

return (num*(num+1)/2);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);

System.out.print("Enter the positive interger number : " );
int n=sc.nextInt();

if(n<0) {
System.out.println("Invalide Integer ! ");
}
else {

int sum=calculateSum(n);
System.out.println("Sum of first " + n + " natural number is : " + sum);
}


}

}