Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Program2{
- public static void main(String[] args){
- Scanner sc = new Scanner(System.in);
- System.out.print("Enter the value of n(1-20): ");
- int n = sc.nextInt();
- if(n >= 1 && n <= 20){
- long term = 1;
- long sum = term;
- for(int i = 2; i <= n; i++){
- term = term + i;
- sum += term;
- }
- System.out.println("The sum is " + sum);
- }
- else{
- System.out.println("Value of n not within range");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement