How do you do exponentiation in C?

前端 未结 7 1503
执笔经年
执笔经年 2020-12-05 06:31

I tried \"x = y ** e\", but that didn\'t work.

7条回答
  •  悲哀的现实
    2020-12-05 06:46

    or you could just write the power function, with recursion as a added bonus

    int power(int x, int y){
          if(y == 0)
            return 1;
         return (x * power(x,y-1) );
        }
    

    yes,yes i know this is less effecient space and time complexity but recursion is just more fun!!

提交回复
热议问题