In this program you`ll learn to display how to print series pattern program like 1.5 + 2.5 + 3.5 + ...+n


Source Code:
  1. public class SeriesProgramPart3 {
  2. public static void main(String[] args) {

  3. try (Scanner var = new Scanner(System.in)) {
  4. double i,n,sum = 0;
  5. System.out.println("Enter the number:");
  6. n = var.nextDouble();
  7. for(i=1.5;i<=n;i++) {
  8. sum = sum +i;
  9. System.out.print(i+" + ");
  10. }
  11. System.out.println("\nThe Summation if series is-"+sum);
  12. }
  13. }

  14. }
Output:

Enter the number:
5
1.5 + 2.5 + 3.5 + 4.5 + 
The Summation if series is-12.0

Enter the number:
10
1.5 + 2.5 + 3.5 + 4.5 + 5.5 + 6.5 + 7.5 + 8.5 + 9.5 + 
The Summation if series is-49.5



0 comments:

Post a Comment