6. Write a Java program to find sum of all natural numbers between 1 to n.
Static :-
class loop6
{
public static void main(String args[])
{
int i=1,j=10,sum=0;
while(i<=j)
{
sum = sum +i;
i++;
System.out.println(sum);
}
}
}
Output :-
import java.util.*;
class loop6
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int i,j,sum=0;
System.out.println("Enter the Stating number :-");
i=in.nextInt();
System.out.println("Enter the Ending number :-");
j=in.nextInt();
while(i<=j)
{
sum = sum + i;
i++;
}
System.out.println(sum);
}
}
Output :-