In this program you`ll learn to display how to print series pattern program like 1.5 + 2.5 + 3.5 + ...+n
Source Code:
- public class SeriesProgramPart3 {
- public static void main(String[] args) {
- try (Scanner var = new Scanner(System.in)) {
- double i,n,sum = 0;
- System.out.println("Enter the number:");
- n = var.nextDouble();
- for(i=1.5;i<=n;i++) {
- sum = sum +i;
- System.out.print(i+" + ");
- }
- System.out.println("\nThe Summation if series is-"+sum);
- }
- }
- }
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