Menu

Read a number from the user and Display sum of first n natural numbers

import java.util.*;
class Sum_of_n_natural_numbers{
public static void main(String args[]){

System.out.print("Enter the number upto the sum is needed");
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int sum=0;

if(n>=1)
{
for(int i=1; i<=n;i++)
{
sum=sum+i;
}
}

System.out.println(sum);


}}