#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   int a, result, factorial(a);
  {
   cout << "please enter a number ";
   cin >> a;
   cout << factorial(a);
  }

   
	for(a=1; a>0; a--)
	   {
		   factorial *=a;
       }
       return 0;
}

Dani AI

Generated

The snippet posted by mixes syntax and logic mistakes: an invalid declaration, an attempt to call a function that does not exist, and a loop that does not compute the factorial as intended. Several responders (, ) spotted misuse of a variable vs a function; attempted a function but the implementation is incorrect. A clear fix is to (1) read a non-negative integer, (2) compute the product with a well-defined function or loop, (3) guard against invalid input and overflow.

A compact, correct approach (iterative, checks input) is below.

#include <iostream>
#include <cstdint>

std::uint64_t factorial(unsigned int n) {
    std::uint64_t result = 1;
    for (unsigned int i = 2; i <= n; ++i)
        result *= i;
    return result;
}

int main() {
    unsigned int n;
    std::cout << "Enter a non-negative integer: ";
    if (!(std::cin >> n)) return 0; // non-numeric input
    if (n > 20) std::cout << "Warning: values above 20 may overflow 64-bit\n";
    std::cout << n << "! = " << factorial(n) << '\n';
    return 0;
}

Practical notes: factorial(0) is 1; reject or handle negative input before computing; 64-bit unsigned holds up to 20! safely—beyond that use a big-integer library (e.g., Boost.Multiprecision) if you need exact results. Prefer iterative implementation in simple tools to avoid recursion depth and stack issues. Finally, compile with a modern C++ standard (for example, g++ -std=c++11) and test with edge cases: 0, 1, 20, and an out-of-range value to confirm behavior.

Recommended Answers

All 5 Replies

Just wondering if you felt like reading the intro threads, and HOW TO POST CODE anytime whilst waiting for an answer.

Code isn't make much sense either. You define factorial as a function but then you multiply 1 and 0 to it like a variable!

And Please help doesn't state the problem. Your code is obviously flawed but we really don't know what the problem you are having is, and exactly what your assignement was!

He already stated his question, "please can any1 help fix this...... ", and I think it's fair we answer it... Yes we can fix this. Hope I was able to help you, xfreebornx. :P

factorial function:

in factorial(int x) {
int t;

for(t = x; t >-;t++)
     x = x*t;

return x;
}

I hope you read into function calling and creation.

commented: 1. Wrong approach to help - do not throws code. 2. Source code in post #5 - is it compiled & executed? -2

Lets see what you are doing :

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int a, result, factorial(a);  //declare 'a' as an int
                                            //declare 'result 'as an int
                                           //declare 'factorial' as in int, initialized to
                                           //the value of 'a' which is ? junk...
{
cout << "please enter a number ";  //ask user for input
cin >> a; //read in the input
cout << factorial(a);    //display factorial(a) ??? I assume you believe
                                   //that factorial is defined in cmath, which is                                                
                                  //not, and if you do then why were you declaring it as a variable?
}


for(a=1; a>0; a--) //start a loop from a = 1 until a <=0, which means
                            //this loop will only execute once 
 {
   factorial *=a;  //now take the variable functions thingy and
                          //multiply it by a, which is 1 , which means the
                         //variable function thingy has not changed
  }
return 0; //return success, as if it were.
}
commented: No need to this post - See OP's post #1. He/She has missed code tags. -2
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.