Why is my power operator (^) not working?

前端 未结 8 1492
半阙折子戏
半阙折子戏 2020-11-22 02:12
#include 

void main(void)
{
    int a;
    int result;
    int sum = 0;
    printf(\"Enter a number: \");
    scanf(\"%d\", &a);
    for( int i =         


        
8条回答
  •  面向向阳花
    2020-11-22 02:44

    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

    double i, x, y, pow;
    
    x = 2;
    y = 5; 
    pow = 1;
    for(i=1; i<=y; i++)
    {
        pow = pow * x;
    }
    
    printf("2^5 = %lf", pow);
    

    You can also simply use pow() function to find power of any number

    double power, x, y;
    x = 2;
    y = 5;
    power = pow(x, y); /* include math.h header file */
    
    printf("2^5 = %lf", power);
    

提交回复
热议问题