gpt4 book ai didi

C++阶乘程序

转载 作者:行者123 更新时间:2023-11-28 06:40:24 25 4
gpt4 key购买 nike

我是这门语言的初学者..这很简单,但我做不对。需要你的帮助

它应该是这样的:

用户输入一个数字,例如5,结果必须是120 (5 * 4 * 3 * 2 * 1)

这是我的代码:

#include<cstdlib>
#include<iostream>

using namespace std;

int main(){

int num;
int prod=0;

cout<<"Enter a number: ";
cin>>num;
cout<<endl;

if(num<1 || num>10){
cout<<"Please enter a number from 1 to 10 only!";
}
else
{
for(int i=num;i>0;i--){
cout<<i;
if(i-1>0){
cout<<"*";
prod = prod * i;
}

}
cout<<" = "<<prod;
}

cout<<endl<<endl;
system("PAUSE");
}

最佳答案

将 prod = 1 而不是零,这将解决您的问题

关于C++阶乘程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26081471/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com