mustish1
asked on
Needs help in syntax
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;
}
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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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;
}
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;
}
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,po wer1);
result = (int)pow((double)number,po
ASKER
Thanks.
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.
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.
ASKER
#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;
}