Home > Back-end >  C file header, please what is the effect of the result of the program is run??
C file header, please what is the effect of the result of the program is run??

Time:09-21

C + + title: if just one number is equal to the sum of its factors, this number is called "the number", for example, six factors for 1, 2, 3, and 6=1 + 2 + 3, 6 is "the number", therefore, programmed to find N within all the number, and according to the following format output factor:

Write a program:
#include
Int main ()
{
int n;
The scanf (" % d ", & amp; N);
for(int i=1; i<=n; I++)
{
Int z=0;
for(int j=1; J<=I/2; J++)
{
If (I % j==0)
Z +=j;
}
If (z==I)
{
Printf (" % d ", z, "its factors are");
for(int j=1; J<=I/2; J++)
{
If (I % j==0)
Printf (" % d ", j);
}
printf("\n");
}
}
return 0;
}

Prompt operation result error, and change the file header and the way of input and output:
#include
#include
using namespace std;
Int main ()
{
int n;
cin> n;
for(int i=2; i<=n; I++)
{
int sum=0;
for(int j=1; J<=I/2; J++)
{
If (I % j==0)
The sum +=j;
}
If (sum==I)
{
Coutfor(int j=1; J<=I/2; J++)
{
If (I % j==0)
Cout}
Cout}
}
return 0;
}

Run correctly,,

This algorithm is not the same in the two pieces of code? Why the first will go wrong? What distinction do their file header some??

CodePudding user response:

Does anyone know?

CodePudding user response:

#include //function declared in the namespace STD
using namespace std;
Or
#include //function declarations in the global namespace

CodePudding user response:

Well, I want to know is that I posted above two code, why a run results will go wrong, right?

CodePudding user response:

The first # include & lt; cstdio> Followed by
using namespace std;

Or change to:
#include

CodePudding user response:

The first no namespace declaration namespace
All the identifiers of the c + + standard library is defined in a namespace called STD
Add a use

CodePudding user response:

Wrong confirm, is compiled, or run,
  • Related