需要帮助我写的程序

需要帮助我写的程序

本文介绍了需要帮助我写的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序来计算一个数字(基数),该数字可以是一个整数,负数或零,而不包括数学库。这就是我到目前为止所写的内容。请帮助我。


#include< iostream>

使用命名空间std;


int main()

{

double x,y;


cout<< 输入基数:;

cin>> x;

cout<< 输入整数指数:

cin>> y;


double i = 1;

double product = x;


while(y> i )

{

产品* = x;

i ++;

}


返回0;

}


我的问题是我如何设置while语句来计算让我们说4提升到-2?我是否在身体中加入了if语句?非常感谢您的帮助。

解决方案



I need to write a program that will compute a number (base) raised to a power which can be an integer, negative or zero with out including the math library. This is what I wrote thus far. Please help me.

#include <iostream>
using namespace std;

int main()
{
double x, y;

cout << "Enter a base number: ";
cin >> x;
cout << "Enter a integer exponent: "
cin >> y;

double i = 1;
double product = x;

while ( y > i)
{
product *= x;
i++;
}

return 0;
}

My problem is how do I set up the while statement to compute lets say 4 raised to the -2? Do I include an if statement in the body? Your help is greatly appreciated.

解决方案




这篇关于需要帮助我写的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:11