//Factoral Thing I made myself

#include <iostream>
using namespace std;
int main ()
{
cout << "*********************"<<endl;
cout << "*Michael F Eversberg*"<<endl;
cout << "*   Factoral Solver *"<<endl;
cout << "*********************"<<endl;
     
     int x, y;
     
     cout << "Please Enter a whole Number: ";
     cin >> x;
     y = x;
     for (x = x-1; x>0; x--) {
        y = y*x;
         }
       cout << y <<endl;
       system("PAUSE");
     return 0;
}

I'm teaching myself C++ basics so I have a better understanding of it when I go off to college. I was on the chapter dealing with control statements and decided to make this as a review tool to review some of what I've learned so far. Tell me what you think.?

Recommended Answers

All 8 Replies

For your information there is nothing recursive about this piece of code. Anyway did you run this and check the results? For example what is the output for input 5?

Member Avatar for iamthwee

I like it where is the recursion though? Is it hidden?

Dono why I said it was recursive...I guess I had that concept stuck in my head because it reuses a previous answer to generate the next multiple.

Also I tested it, for example 5! is 120, and the program generates 120 as an answer.

Out of curriosity, how are factorals applied? I've never had to use them out side of math class, and we where never given any real world applications for them.

Member Avatar for iamthwee

Dono why I said it was recursive...I guess I had that concept stuck in my head because it reuses a previous answer to generate the next multiple.

Oh ok, it works nice, it's factorial by the way.

Dono why I said it was recursive...I guess I had that concept stuck in my head because it reuses a previous answer to generate the next multiple.

Also I tested it, for example 5! is 120, and the program generates 120 as an answer.

Out of curriosity, how are factorals applied? I've never had to use them out side of math class, and we where never given any real world applications for them.

here

The main use of the factorial function is that it's the Hello World of recursive functions. Along with the Fibonacci numbers, it is the first example used in many classes.

Computing individual factorials is usually not useful. But understanding the concept may be useful. They are often found in formulas such as sum n=1..N ((x^n)/n!), which approximates e^x.

If you were to approximate the result of this formula, you would not want to recompute 1!, 2!, 3!, ..., N! over and over again; you'd want to compute the products incrementally. If you have written a bare factorial algorithm before, this would not be hard to do.

If you see code that contains and actually uses a factorial function, it is probably written inefficiently. Somewhat more useful is a 'falling power' function, where falling_power(n, k) = n * (n - 1) * (n - 2) * ... * (n - (k - 1)) = n! / k!. The formula n!/k! is a convenient way of writing and manipulating the falling power (also known as n `permute` k), but it is not the most efficient means of computation.

The only time a factorial function is not an inefficient way to compute things is when you've computed exactly one factorial; otherwise, intermediate results could be used to compute further factorials.

Out of curriosity, how are factorals applied? I've never had to use them out side of math class, and we where never given any real world applications for them.

Look through uses of mathematics, such as breaking CAPTCHAs (that's just one example of a use of mathematics; it's not necessarily true that factorial is even computed indirectly in that process).

Many formulas for approximating things like areas underneath the normal distribution use factorials (even double factorials!), and I personally found this useful in making an in-game bot that calculates and updates player ratings for one-on-one combat.

But the reason you're learning it right now is for learning ways of writing algorithms.

I think...I should walk away from programming...that was not for my brains...

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.