Program to calculate the n factorial, C/C++ Programming

Debug the following program to calculate N!

#include

using namespace std;

main()

{

            int N, factorial=1;

            cout << "Enter a positive integer"

            cin >> N;

            for(int i=N; i>=1; factorial *= --i);

            cout << N<<"!="<

return 0;

}

What was wrong?

Set N='A'. What is the result? Does it make sense? Modify the program to make sure that the entered value is numeric. You can do so by using cin.fail() and cin.clear() (see the following for more information.

Also, modify the program such that it does not accept negative numbers and insists on getting a positive value for N. You can do so by using a while loop that keeps asking for a positive integer till a positive integer is entered.

Get your code to compute 1!,2!,5!, and 25!. Are you getting the correct answer for all of the entered values? If not, what went wrong? Would you be able to fix this by changing the variable type? Explain your answer.

Posted Date: 2/14/2013 4:33:58 AM | Location : United States







Related Discussions:- Program to calculate the n factorial, Assignment Help, Ask Question on Program to calculate the n factorial, Get Answer, Expert's Help, Program to calculate the n factorial Discussions

Write discussion on Program to calculate the n factorial
Your posts are moderated
Related Questions
Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve between two points can b


Explain about the Integer constants in c language? An integer constant is the integer valued number and it refers to a sequence of digits. A decimal integer constant includes o

i have an array of structs, and am trying to make a pointer that can point to a certain struct within the array

Yes. "Memory pools" are useful in many situations. The bad news is that I'll need to drag you through the mire of how it acts before we talk about all the uses. Firstly, recall

Explain procedural logic A procedure, or function, was explained as a set of specific instructions implemented in sequential manner. The data is kept separate from the procedur

check this link: http://cactus.eas.asu.edu/partha/Teaching/430.2013/Project/proj-2.2013.htm

Byteland county is very famous for luminous jewels. Luminous jewels are used in making beautiful necklaces. A necklace consists of various luminous jewels of particular colour. Nec

E x p r e s sion and their types: An expression will be in form of mathematical expression with C++ syntax embedded with it. Expressions are of following types which m

Reference Types The symbol "&" is interpreted as an address operator as well as AND operator. This operator is also used to declare a "reference variable". A reference is refer