sorry to bother you all again, but I am struggling with powers in c.
#include %26lt;iostream%26gt;
int main(){
for (int i = 0; i %26lt; 20; i++){
printf("2 ^ %d = %d \n", i, 2^i);
}
}
returns
2 ^ 0 = 2
2 ^ 1 = 3
2 ^ 2 = 2...
what am I doing wrong? and what is the ^ opperator if not power?
Powers in c?
Why don't you use the function pow(x,y) in %26lt;math.h%26gt;??
"^" is a bitwise operator (XOR)
Reply:^ I think is a bit-wise operator in C.
There is no exponent function native in C unless one's burried in the math libraries.
But I think this excersize is supposed to force you to create the function, passing in x and y).
Reply:#include %26lt;iostream%26gt;
#include%26lt;math.h%26gt;
int main(){
for (int i = 0; i %26lt; 20; i++){
printf("2 ^ %d = %d \n", i, pow(2,i));
}
}
try to run this program you will get correct answer.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment