Question

When i run the following code

/*Program to find the greatest common divisor of two nonnegative integer
 values*/


#include <stdio.h>

int main(void){
    printf("    n  |  n^2\n");
    printf("-----------------\n");
    for(int n = 1; n<11; n++){
        int nSquared = n^2;
        printf("%i          %i\n",n,nSquared);
    }
}

The table that gets returned to the terminal displays as follows

    n  |  n^2
-----------------
1          3
2          0
3          1
4          6
5          7
6          4
7          5
8          10
9          11
10          8

why does the "n^2" side generate the wrong numbers? And is there a way to write superscripts and subscripts in C, so I do not have to display "n^2" and can display that side of the column as "n²" instead?

Was it helpful?

Solution

Use pow function from math.h.

^ is the bitwise exclusive OR operator and has to nothing to do with a power function.

OTHER TIPS

The ^ is the XOR operation. You'd either want to use the math.h function "pow", or write your own.

^ is the bitwise xor operator. You should use the pow function declared in the math.h header.

#include <stdio.h>
#include <math.h>

int main(void) {
    printf("    n  |  n^2\n");
    printf("-----------------\n");
    for(int n = 1; n < 11; n++){
        int nSquared = pow(n, 2); // calculate n raised to 2
        printf("%i          %i\n", n, nSquared);
    }
    return 0;
}

Include the math library by the flag -lm for gcc compilation.

As others have pointed out, the problem is that ^ is the bitwise xor operator. C has no exponentiation operator.

You're being advised to use the pow() function to compute the square of an int value.

That's likely to work (if you're careful), but it's not the best approach. The pow function takes two double arguments and returns a double result. (There are powf and powl functions that operator on float and long double, respectively.) That means that pow has to be able to handle arbitrary floating-point exponents; for example, pow(2.0, 1.0/3.0) will give you an approximation of the cube root of two.

Like many floating-point operations, pow is subject to the possibility of rounding errors. It's possible that pow(3.0, 2.0) will yield a result that's just slightly less than 9.0; converting that to int will give you 8 rather than 9. And even if you manage to avoid that problem, converting from integer to floating-point, performing an expensive operation, and then converting back to integer is massive overkill. (The implementation might optimize calls to pow with integer exponents, but I wouldn't count on that.)

It's been said (with slight exaggeration) that premature optimization is the root of all evil, and the time spent doing the extra computations is not likely to be noticeable. But in this case there's a way to do what you want that's both simpler and more efficient. Rather than

int nSquared = n^2;

which is incorrect, or

int nSquared = pow(n, 2);

which is inefficient and possibly unreliable, just write:

int nSquared = n * n;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top