C++ program to calculate exponent


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: C++ program to calculate exponent

  1. #1
    Join Date
    Mar 2005
    Posts
    2

    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
    }

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

  2. #2
    Join Date
    Dec 2003
    Posts
    3,366
    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.

  3. #3
    Join Date
    Jul 2004
    Posts
    4
    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);

  4. #4
    Join Date
    Mar 2005
    Posts
    2
    Ok, i now know what i need to do. Thanks for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center