
Power and Factorial
My program basically asks the users for a value (s) then calculates and displays the First, Middle, Last, and Final. The users have a choice to enter the value either in integer, real or character. If the user enters it in real or character, it will be converted into integer value before the required value is computed. I have written the power , factorial and the sigma functions which I will use in calculating the first, middle and the last. Can somebody assist in checking my codes to see if its correct and show how I will those functions to calculate the first and the last which a bit more complicated?
First = X^1/1!  X^3/3! + X^5/5!  X^7/7! + X^9/9!  ....X^n/n!
Middle = n sigma i = 1 sqrt(i/x)
last = 1 X^2/2! + X^4/4!  X^6/6! + X^8/8!  ....X^n/n!
Final = first/last + middle
Code:
#include<iostream>
#include<cmath>
using namespace std;
int powerfunc (int x, int n)
{
int x, n;
int p, i;
while (i <=n)
{
p = p * x;
i = i + 1;
return p;
}
}
int factorial (int n)
{
int i, n;
int f = 1;
for (i = 1; i <= f; i++)
{
f *= i;
return f;
}
}
int sigma(int x, int n)
{
int i = 1, sum = 0;
int x, n;
while (i <= n)
{
sum += i * i;
i++;
sum = sqrt(sum/x)
return sum;
}
}
int main()
{
int x, n;
int first, middle, last, final;
do {
cout << “Please enter the value for x” << endl;
cin >> x;
cout << “Please enter the value for n” << endl;
cin >>n;
middle = sigma(x, n)
final = fist/(last + middle);
cout << “The value for first is << first << endl;
cout << “The value for middle is << middle << endl;
cout << “The value for last is << last << endl;
cout << “The value for final is << final << endl;
cout << "Do you want to calculate this again? (y/n): ";
cin >> ans;
} while (ans == 'y'  ans == 'Y');
..
getch ();
return 0;
}

Well you have return statements inside your loops.
So you're only running the first iteration.
Better indentation would help you spot some of these problems.

You would have picked up a lot of concerns if you actually tried to compile and run your program. A compiler will complain bitterly on some parts of your code. If you get the program running, you will get results you don't expect.
For example, look a bit more closely at your sigma() function. It redeclares x (and n) inside the function. Having a function with an argument named x and a variable inside the body of that named x causes some ambiguity about what "x" actually refers to. You're also returning from inside your while() loop, which means you only execute the loop body once.
An int is a bounded type (i.e. there is a maximum value it can hold). Raising a value to a power or computing a factorial can easily produce a result that exceeds what can be stored in an int (regardless of what size int your compiler actually supports). If your compiler supports a 32 bit int (typical of many compilers, but not universal) overflow will occur if trying to compute factorial of 13 or so. Because of that, you'll need to consider some smart way of computing a term x^n/n!
sqrt() works with a floating point (double) type. Dividing two integers gives an integer result. Truncation of some form will be happening which may, or may not, be what you want. There is no magic that makes the division of two integers become a real number.

Whatever they don't spot, maybe someone else can find through crossposting

Yeah  just found that :mad:

Fix your indentation and compile the thing fist, you lazy so and so.
Test the functions one at a time before combining them.