Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. import java.util.Scanner;
  2. class Program2{
  3. public static void main(String[] args){
  4. Scanner sc = new Scanner(System.in);
  5.  
  6. System.out.print("Enter the value of n(1-20): ");
  7. int n = sc.nextInt();
  8.  
  9. if(n >= 1 && n <= 20){
  10. long term = 1;
  11. long sum = term;
  12. for(int i = 2; i <= n; i++){
  13. term = term + i;
  14. sum += term;
  15. }
  16. System.out.println("The sum is " + sum);
  17. }
  18. else{
  19. System.out.println("Value of n not within range");
  20. }
  21. }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement