Computer Science, asked by Nayana35816, 3 months ago

Write a Java program to print the sum of the following series: 1-4+9-16+25

Answers

Answered by anindyaadhikari13
3

Answer:

This is the required Java program for the question.

import java.util.*;

public class Java {

 public static void main(String[] args) {

     int i,n,s=0,c=1;

     Scanner sc=new Scanner(System.in);

     System.out.print("Enter limit - ");

     n=sc.nextInt();

     for(i=1;i<=n;i++)  {

        s+=c*i*i;

        c*=-1;

     }

     System.out.println("Sum: "+s);

     sc.close();

 }

}

Algorithm:

  1. START.
  2. Initialise sum=0, c=1.
  3. Ask the user to enter the limit, say n.
  4. Iterate a loop in the range I = 1 to N.
  5. Add the value of c*i² to sum.
  6. Multiply the value of c by -1 after each iteration as terms are added and subtracted.
  7. Display the sum.
  8. STOP.

See the attachment for output ☑.

Attachments:
Similar questions
Math, 9 months ago