Dear all,

There is some errors in the code that I can’t seem to spot.

The code is

# include <iostream> 
using namespace std;
int main () 
{

int N;       // Declaring and initializing variables
int n=0; 
int i=0; 
int fact=1; 

cout << "Please Enter an Integer" << endl; // The prompt
cin >> N; // To read from the keyboard

// Part a
 
if (N==1 || N==0) 
{ cout << "The Factorial of " << N << " = 1" << endl; } // Because it's known that the fact. of 0/1 = 1


while (N>1 && i>=0)

 { fact = fact * (N-i);
     i = i+1;
   }


cout << "The factorial of " << N << " = " << fact << endl;

// Part b

for ( int sigma=0; n=0 || n<=N ; n++)
{ sigma = sigma + (n*n);
}

 
return 0;
 
 
}

and the question is attached

Recommended Answers

All 12 Replies

If you are debugging code (and that is what you are doing here), you need to step through the code and check your assumptions. That can be done with a traditional debugger that lets you see each line as it is run, or by putting some output statements in the code.

Your errors include
(a) the loop for the factorial never is run. since neither N nor i are changed in the while loop. This should be fairly obvious since you never reached the "factoraila == " statement

(b) you are using a loop to calculate [tex]\sum n^2[\tex] ?? There are simple formula to get the result, no loop.

If you are debugging code (and that is what you are doing here), you need to step through the code and check your assumptions. That can be done with a traditional debugger that lets you see each line as it is run, or by putting some output statements in the code.

Your errors include
(a) the loop for the factorial never is run. since neither N nor i are changed in the while loop. This should be fairly obvious since you never reached the "factoraila == " statement

(b) you are using a loop to calculate [tex]\sum n^2[\tex] ?? There are simple formula to get the result, no loop.

How come it is not changing in part (a) I did increament it

(b) I am using the baisc concept of it "Mathematically ... should it be altered somehow ?

//Any help, remarks would be appreciated

while (N>1 && i>=0)

 { fact = fact * (N-i);
     i = i+1;
   }

Yes, i changes, but not in a way that will get you out of this loop. N doesn't change, so this is an infinite loop. Once inside, you'll never get out (well, eventually, after i overflows, you might possibly get out, but that'll take a few billion iterations). Change the conditions on your while loop so that it is no longer an infinite loop.

For me, in these situations, its always easier to set up a for loop.

For instance you could have...

for(int i = 1; i <= n; i++)
{
// Factorial code goes here
}

This way the code in the for loop keeps repeating until i is greater than n, which solves your problem of an infinite loop.

while (N>1 && i>=0)

 { fact = fact * (N-i);
     i = i+1;
   }

Yes, i changes, but not in a way that will get you out of this loop. N doesn't change, so this is an infinite loop. Once inside, you'll never get out (well, eventually, after i overflows, you might possibly get out, but that'll take a few billion iterations). Change the conditions on your while loop so that it is no longer an infinite loop.

aha ... so the problem is with the condition .. exactly in using (&&) ?

And how come part b is not working ??

aha ... so the problem is with the condition .. exactly in using (&&) ?

No its not just the &&, its the whole statement. You need to rethink your condition. The one you have now is incorrect. Take a look at my earlier post for ideas.

I did correct it. That was for part a

and I modified part b .. as follows

// Part b
cout << "Please Enter an Integer" << endl; // The prompt
cin >> N; // To read from the keyboard


sum=0;
for ( n=0 ; n<=N ; n++)
{ 
	sum = sum + (n*n);
}

return 0;


}

It did not use to appear so I thought I would add the prompt again

the prompt appeared and I entered the number but then it terminates

I think I don't have to put the prompt again .. maybe there's something wrong with the code at part b .. any hints?

It doesnt appear that you have sum declared anywhere, unless you included it in the part A of the code. Also, you are not telling the program to do anything after the loop takes place. Output the variable sum after the for loop to see if your program actually works. ;)

Hi, thanks for replying

well actually I started doing it all again "FOCUSING" lol

it works just fine now


Thanks all

Yes sometimes a little focus can go a long way. Please mark the thread solved if you have no further questions. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.