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
Objective:  Construct a C program that controls the LCD display, and is capable of displaying strings.  Add functions to the C program that allows more control over the display.

write a program that can accept numbers and perform all the four arithmetic operation

Multiple Inheritance Multiple inheritance , as the name suggests , is  deriving a class from more than one class . The derived class inherits all the properties of all its base c

Write a C program to compute the factorial of  5 i.e. 1x2x3x4x5 etc Write a C program to calculate the output Y for a given value of X for the following formula.       Y=X 2

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

How would I use variables of a C++ object within a Lua function, and then call the Lua function from C++ code?

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

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

Define a user-defined data type (struct) called Car to represent a car in the context of a car dealership database. Car must have at least the following characteristics: Make Model

The Scope Resolution Operator( :: ) Global variables are explained outside any functions and thus can be used by all the functions defined thereafter. However, if a global vari