Write a program to find the sum of the series:
s= 1- a + a2
- a3+ ---------------+a10 in java
. write accurately I have to type
Answers
Answered by
0
Answer:
important java. util. *
public class series
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in) ;
inter a, s, i;
System.out.println("Enter a no:") ;
a=in.nextInt() ;
for(i=1;i<=10;i++)
{
if(i%2! =0)
{
s=s-(a*i) ;
System.out.println(s) ;
}
else
{
s=s+(a*i) ;
System.out.println(s) ;
}
}
}
}
Explanation:
mark me brainliest for my hardwork
Answered by
3
Correct Series:-
Required Answer:-
import java.util.*;
class Series{
public static void main (String ar []){
Scanner sc=new Scanner (System.in);
System.out.println("Value of a?");
int a=sc.nextInt();
double s=0;
for(int I=0;I<=10;I++)
s=(i%2==0)?(s+(Math.pow(a,I))):(s-(Math.pow(a,I)));
System.out.println("Sum="+s);
}
}
Note:-
- You can also write
if (i%2==0)
s+=Math.pow(a,I));
else
s-=Math.pow(a,I))
Logic:-
- Initialise s as 0 and accept value of a as input.
- Run a loop from 0 to 10
- Obviously, any number raised to power 0 will be 1 so at first iteration s will get 1
- Second iteration, I becomes 1 and 1%2==0 condition false so control will execute else part
- Next iteration, I becomes 2 and 2 %2==0, condition true, control executes the if part.
- After loop gets over, display sum
- Woohoo! You made the program!!!
•••♪
Similar questions