Write a Java program to print the sum of the following series: 1-4+9-16+25
Answers
Answered by
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:
- START.
- Initialise sum=0, c=1.
- Ask the user to enter the limit, say n.
- Iterate a loop in the range I = 1 to N.
- Add the value of c*i² to sum.
- Multiply the value of c by -1 after each iteration as terms are added and subtracted.
- Display the sum.
- STOP.
See the attachment for output ☑.
Attachments:
Similar questions
Sociology,
1 month ago
Math,
1 month ago
Math,
3 months ago
Math,
9 months ago
India Languages,
9 months ago