Example
Input
5
Output
15
Required to know
Use of Variables, Use of different operators like assignment or arithmetic operators, Use of methods like System. out. print(), and use for loop, and Use of Data types
Steps
Take range as input from the user.
In this case, we have to initialize one variable as 0, to make sure that the addition of the numbers takes place.
The loop should run until the number is more than the range. Now one thing to do is to print the sum outside the loop. Otherwise, you may get the result but it may come in this format.
1 3 6 10 15
Program
import java.util.Scanner;
public class Mavenproject1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n;
int sum = 0;
System.out.print("Enter the number");
n = sc.nextInt();
for(int i=1;i<=n;i++){
sum = sum + i;
}
System.out.print(sum + " ");
}
}