QN016. Write a program to print sum of n1 to n2.
1. Objective
To print the sum of numbers from two given numbers by the user.
2. Theory
In this program, we ask for the user to input two numbers to generate list of numbers and use Scanner class to store the given input. Initialize sum variable as 0 at the beginning to store the sum of all numbers and print the result.
3. Source Code
      import java.util.Scanner; 
      public class Qn016{
        public static void main(String[] args){
          int sum = 0;
          Scanner input = new Scanner(System.in);
          System.out.print("Enter first number: ");
          int n1 = input.nextInt();
          System.out.print("Enter second number: ");
          int n2 = input.nextInt();
          for(int i=n1;i<=n2;i++)
          {
            sum+=i;
          }
          System.out.println("Sum of " + n1 + " to " +  n2 + " is " + sum);
        }
      }
    
    4. Output
      Enter first nubers: 500 
      Enter second number: 600 
      Sum of 500 to 600 is 55550
    
5. Conclusion
In this way, I would like to conclude that, we can ask any two numbers from user and calculate the sum from first number to second number.