在 C 程序中不用乘法 (*) 和除法 (/) 操作符编写自己的幂函数
幂函数使用乘法计算,即 5n 是 5*5*5… n 次。对于此函数,要在不使用乘法 (*) 和除法 (/) 操作符的情况下正常工作,我们将使用嵌套循环来多次添加数字。
示例
#include <iostream>
using namespace std;
int main() {
int a= 4 , b = 2;
if (b == 0)
cout<<"The answer is"<<1;
int answer = a;
int increment = a;
int i, j;
for(i = 1; i < b; i++) {
for(j = 1; j < a; j++) {
answer += increment;
}
increment = answer;
}
cout<<"The answer is "<<answer;
return 0;
}输出
The answer is 16
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP