write a program in java to find and display the sum of the following series s=1/a+1/a^2+1/a^3+............. +1/a^n
Answers
Answered by
4
Question:-
Write a program in java to find and display the sum of the series.
S=1/a +1/a^2 +1/a^3 +......
Program:-
import java.util.*;
class Sum
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the value of a: ");
int a=sc.nextInt();
double s=0.0;
if(a==0) // checks if a is 0 or not.
{
System.out.println("Invalid Input..Please try again. ");
System.exit(0);
}
else
{
int d=1;
for(int i=1;i<=n;i++)
{
s+=1/Math.pow(a, d);
d+=2;
}
}
System.out.println("Sum of the series is: "+s);
}
}
Similar questions