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 main program that uses these system calls to echo the typed characters. The pseudo code will look something like: void traphandler ()  {    if (R0 == 0) { // read sys

Explain the Bit Wise Operations? C language in particular was created to make it easier to write operating systems. The objective was to have a language that provided excellent

I need Decompile Library(.DLL or .SO) into source code Project Description: I am seeking an experienced programmer or anyone who knows a thing or two about reverse engineerin

In this problem you are given a board in which some of the elements are placed as shown in diagram below. Each element represents a color. Fill the other elements in the board, suc

Write C code for a program that implements a digital clock (HH:MM:SS). The clock includes a two-digit hour indicator HH (00-23), a two-digit minute indicator MM (00-59), and a t

THIS PROGRAM IS TO ASK A DATE & ALSO ASK FOR DAYS TO BE ADDED TO PRODUCE #include #include struct date  {   int dd;   int yy;   int mm;  }; void main()  {  clrscr();  in


Blanche has a fashion design company called BLB_Best_Clothing Pty. That she has just opened recently

Explain the terms- Substitutability and Extensibility Substitutability - The objects of a properly derived class can be safely and easily substituted for an object of its

Constructor and Destructor function with derived classes If there are constructors included in the base class and the derived class, the compiler automatically calls both of th