Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. package com.javamultiplex.number;
  2.  
  3. import java.util.Scanner;
  4.  
  5. /**
  6. *
  7. * @author Rohit Agarwal
  8. * @category Questions on Number
  9. * @problem How to generate first N triangular numbers?
  10. *
  11. */
  12. public class TriangularNumbers {
  13.  
  14. public static void main(String[] args) {
  15.  
  16. Scanner input = null;
  17. try {
  18. input = new Scanner(System.in);
  19. System.out.println("Enter value of N : ");
  20. int limit = input.nextInt();
  21. int triangular = 0;
  22. System.out.println("Triangular numbers are : ");
  23. for (int n = 1; n <= limit; n++) {
  24. triangular = (n * (n + 1)) / 2;
  25. System.out.print(triangular + " ");
  26. }
  27. } finally {
  28. if (input != null) {
  29. input.close();
  30. }
  31. }
  32.  
  33. }
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement