write a program in java to calculate and print the sum of the following series s= x raise to the power 1 + x raised to the power 3 + x raise to the power 5 + x raise to the power 7+ ...............x raise to the power n. Take necessary inputs from the user.
Answers
Question:-
Write a program to calculate and display the sum of the following series.
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 x: ");
double x=sc.nextDouble(), s=0.0;
System.out.print("Enter the value of n: ");
int n=sc.nextInt(), a=1;
for(int i=1;i<=n;i++,a+=2)
s+=Math.pow(x, a);
System.out.println("The sum of the given series is: "+s);
}
}
Answer:
HERE'S IS YOUR ANSWER
Explanation:
import java.util.*;
class Prg {
void main() {
Scanner sc = new Scanner(System.in);
int i,n ,x , sum=0;
System.out.println("Enter the VALUE of N: ");
n = sc.nextInt();
System.out.println("Enter the VALUE of X: ");
x = sc.nextInt();
for(i=1;i<=n;i+=2) // i=i+2 is written in short cut as i gets incremented 2 times
{
sum += (int)Math.pow(x,i); // DOUBLE VALUE IS TYPE-CASTED TO INTEGER
}
System.out.println("SUM: "+sum);
}
}
PLZ MARK ME BRAINLIEST