#include "stdio.h"
#include "math.h"
void main(void)
{
int a;
int result;
int sum=0;
printf("Enter a Number : ");
scanf("%d",&a);
for(int i=1;i<=4;i++)
{
result = a^i;
sum =sum+result;
}
printf("%d\n",sum);
}
I don't know why this '^' is not working as power.
First of all
^
is a Bitwise XOR operator not power operator.You can use other things to find power of any number. You can use for loop to find power of any number
Here is a program to find x^y i.e. xy
You can also simply use pow() function to find power of any number
Well, first off, the
^
operator in C/C++ is the bit-wise XOR. It has nothing to do with powers.Now, regarding your problem with using the
pow()
function, some googling shows that casting one of the arguments to double helps:Note that I also cast the result to
int
as allpow()
overloads return double, notint
. I don't have a MS compiler available so I couldn't check the code above, though.Since C99, there are also
float
andlong double
functions calledpowf
andpowl
respectively, if that is of any help.Instead of using
^
, use 'pow' function which is a predefined function which performs the Power operation and it can be used by includingmath.h
header file.^
This symbol performs BIT-WISE XOR operation in C, C++.Replace
a^i
withpow(a,i)
.include math.h and compile with gcc test.c -lm
In C
^
is the bitwise XOR:There's no operator for power, you'll need to use
pow
function from math.h (or some other similar function):pow() doesn't work with
int
, hence the error "error C2668:'pow': ambiguous call to overloaded function"http://www.cplusplus.com/reference/clibrary/cmath/pow/
Write your own power function for
int
s: