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
Function Specification:
int calculate(int,int);
where the first argument corresponds to the value of x and second corresponds to n.
Note:
Please use the same prompt statements given in the sample input and output
Input Format:
The first line of the input contains an integer x.
The second line of the input contains an integer n (n>=0).
Output Format:
Output displays an integer.
Note: If n = 0, print 1.
Refer sample input and output for formatting details.
[ All text in bold corresponds to Input and the rest output ]
Sample Input and Output:
Enter the value of x
2
Enter the value of n
3
The result is
14
Function Definitions:
int calculate (int,int)
Solution
#include <iostream>
#include <cmath>
using namespace std;
int calculate(int x, int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
{
sum = sum + pow(x, i);
}
return sum;
}
int main()
{
int x, n;
cout << "Enter the value of x\n";
cin >> x;
//sauravhathi
cout << "Enter the value of n\n";
cin >> n;
cout << "The result is" << endl;
if (n == 0)
{
cout << "1" << endl;
}
else if (n > 0)
{
cout << calculate(x, n) << endl;
}
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.