Home > Back-end >  Small white for help! Why doesn't the c program is run right
Small white for help! Why doesn't the c program is run right

Time:09-18

#include
#include
using namespace std;
Int main ()
{
Double a; Double n; Double s=0; Double b=1;

For (a=1; A. & lt;=20;//a +) increased from 1 to 20
{
For (n=1; N & lt;=a; N++)//computing a!=b
B=b * n.
}
S=s + b;
Cout & lt; system("pause");
}
Calculate 1! + 2! + 3! + 20 +,,,!

CodePudding user response:

Will double to int a try,

CodePudding user response:

S=s + b; Should be placed on the outer for loop,
Double can change to unsigned long or unsigned long long, should be enough,
But the efficiency is not high, such as 20!==20 * (19! , before the next class number calculation can use a number of classes as a result,

CodePudding user response:

There are b to initialise each time,

CodePudding user response:

S=s + b should be in the first layer of circulation in the body, you put it in the outside, put it outside, it is not n factorial b, but 20 of factorial,

CodePudding user response:

Everyone has been explained clearly upstairs
  • Related