# C++ program to calculate exponent

• 03-24-2005, 10:50 PM
berrysnick
C++ program to calculate exponent
Please help, I need to write a program to calculate exponent. Below is what I have. I don't even know where to begin.

**************************************

Declare x as float
Declare y as integer
Declare i as integer
Declare result as float

Input x // can be any float
Input y // has to be a positive integer

Set result = 1 // initial value

For i = 1 to y
result = result * x

End for

Output result

**********************************************

#include <iostream>

using namespace std ;

int main()
{
Write code to declare all the variables.

// read in the two numbers

cout << endl ;
cout << "Enter number to be raised to power(float) : ";

cin >> y ;
cout << "Enter the exponent (positive integer) : " ;
cin >> x ;

Initialize any variables that need to be initialized.

for ( )
{

Write the code for the for-loop initializations
and the for-loop body. Carefully study and compare
the pseudocode and C++ for loops in the commentary.

}

// display the result

cout << endl ;
cout << "The value of" << x << " to power " << y
<< " is : " << result << endl ;

return (0); // terminate with success
}

*****************************************************
• 03-24-2005, 10:54 PM
jonnin
The top comment tells you exactly what to do!
a variable is declared by
type name;
so
float x;
int number;
etc...

But you need to sit down and read your book for the class, and learn how to write a loop, etc.
• 03-27-2005, 12:48 AM
syntax-error
You could write a function that can do the math for you. Here is an example:

int powerFunc(int x, int y)
{
for (int i = 0; i < x; i++)
{
sum *= y; // here it will keep mulitplying itself until
// the condition is met
}

return (sum);
}

your main would look like this:

int result = powerFun(x,y);
• 03-30-2005, 12:07 AM
berrysnick
Ok, i now know what i need to do. Thanks for your help.