Expression Evaluation: In the Mini project 7th module is to evaluate the expression y = x + x^2+……..+x^n. Rita allotted this function to Malini. Collect and display the values returned by the called function. Test your program and report the results obtained.
Help Malini to write a program to evaluate the expression?
Get the value of x and n from the user as input
Use the following function:
int calculate(int x,int n)
where the first argument corresponds to the value of x and second corresponds to n respectively.
Sample Input and Output :
Enter the value of x
2
Enter the value of n
3
The result is
14
Solution
import java.util.*;
import static java.lang.Math.*;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the value of x");
int x = in.nextInt();
System.out.println("Enter the value of n");
int n = in.nextInt();
//sauravhathi
System.out.println("The result is\n"+calculate(x, n));
}
public static int calculate(int x, int n) {
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum = sum + (int)pow(x, i);
}
return sum;
}
}Happy Learning – If you require any further information, feel free to contact me.
![[Solved] Expression Evaluation with Java [Solved] Expression Evaluation with Java](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Expression-Evaluation-with-Java.png)
![[Solved] Strange Median Contest Problem](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Strange-Median-Contest-Problem-300x200.png)
![Write a class ElementCheckInEveryPair with a public method checkElement that takes two parameters one is arr of type int[] and second one is arg of type int and returns true if every pair of arr contains at least one arg](https://realcoder.techss24.com/wp-content/uploads/2022/06/Write-a-class-ElementCheckInEveryPair-with-a-public-method-checkElement-that-takes-two-parameters-one-300x200.png)
![[Solved] Sum of Odd and Even Digits with Java, C++, Python](https://realcoder.techss24.com/wp-content/uploads/2022/07/Solved-Sum-of-Odd-and-Even-Digits-with-Java-C-Python-300x200.png)