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

Debug the following program to calculate N!


using namespace std;



            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 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
how to find minimum number if shelves required

program to convert string from super ascii string with minimum cost

Need Cron Job Parsing JSON from API, Inserting in to DB Project Description: The Project is to prepare a Cron Job with an adjustable interval in seconds and milli seconds. Cr

At a shop of marbles, packs of marbles are prepared. Packets are named A, B, C, D, E …….. All packets are kept in a VERTICAL SHELF in random order. Any numbers of packets with thes

I need Asp .net Facebook app project completion Project Description: We are urgently seeking a few remaining items to be completed, and the project to be deployed to live.

write a program in c car booking system

Define the Recursion Function in C? In C, it is potential for the function to call themselves a function is describing 'recursive' if a statement within the body of a function

What are control structures? - Control structures decide which instructions in program must be executed. - This implies that program flow may not necessarily move from one s

Overloading Unary Operators class sign {int a,b,c; public: sign(){}; sign(int,int,int); void putdata(void); void operator-(); }; void sign::operator-() {a=