Solved

Needs help in syntax

Posted on 2011-02-23
6
360 Views
Last Modified: 2012-05-11
Hi guys: Can any one please tell me how to raise the power of any number for eg. 2 raise to power 3 = 8

Thanks.

#include <iostream>
using namespace std;

int main()
{
      int number = 0;
        int power1 = 0;
        cout << "Enter Number to raise==>";
        cin >> number;
        cout << "Enter Power==>";
        cin >> power1;
        cout << number^power1;  // syntax error
      system("pause");
      return 0;
}  
0
Comment
Question by:mustish1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 

Author Comment

by:mustish1
ID: 34966974
I fix the syntax but it still wrong answer

#include <iostream>
using namespace std;

int main()
{
      int number = 0;
        int power1 = 0;
        int result = 0;
        cout << "Enter Number to raise==>";
        cin >> number;
        cout << "Enter Power==>";
        cin >> power1;
        result = number^power1;
        cout << result << endl;
      system("pause");
      return 0;
}  
0
 
LVL 7

Accepted Solution

by:
MrNed earned 500 total points
ID: 34967000
0
 

Author Comment

by:mustish1
ID: 34967012
Why it gives syntax error
  result = pow(number,power1);

-----------------------
#include <iostream>
#include <math.h>
using namespace std;

int main()
{
      int number = 0;
        int power1 = 0;
        int result = 0;
        cout << "Enter Number to raise==>";
        cin >> number;
        cout << "Enter Power==>";
        cin >> power1;
        result = pow(number,power1);
        cout << result << endl;
      system("pause");
      return 0;
}  
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 7

Expert Comment

by:MrNed
ID: 34967026
No idea about a syntax error, but check your types. It returns a float/double and the first parameter might need to be casted to a double. Try this:

result = (int)pow((double)number,power1);
0
 

Author Closing Comment

by:mustish1
ID: 34967036
Thanks.
0
 
LVL 8

Expert Comment

by:crysallus
ID: 34967039
It might not know which version of the function to call as your number variable is an int, which doesn't match any of the pow methods exactly. Try:

result = pow((double)number, power1);

which casts the variable number to a double before calling the function, which specifies that it should call that version of the pow function.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Simple Linear Regression

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question