Factorial Finder

What's wrong with this application? I want the user to enter a number and it finds the factorial of it by displaying the result on the screen.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

// A recursive function is a function calling itself
// A basecase is an ending point for the function

using namespace std;

int factorialFinder(int x);

int main(){

    cout << "Enter a number: ";
    cin >> factorialFinder();

    cout << "Factoral is " << factorialFinder() << "\n";

    return 0;
}

int factorialFinder(int x){
    if (x == 1){
        return 1; // basecase
    }else{
        return (x * factorialFinder (x - 1));
    }

}



What is it

cin >> factorialFinder();

???
So, that it gets a number from the user and acts accordingly with the function.
Where are you entering number?
You need declare a variable and then use it to extract input and pass to the function.
Can you show me how its done amhndy? I already had this issue a few days ago.
1
2
3
unsigned int num;
std::cin >> num;
std::cout << myFunction(num);
Last edited on
And your factoral function is invalid. It returns incorrect values for x <= 0.
Factorial of a negative number !!! I hope you were kidding or being sarcastic.

About the program there is one catch, you need to return 1 if input is 0 and change int to unsigned int or add check for negative input.
Last edited on
@amhndu

Factorial of a negative number !!! I hope you were kidding or being sarcastic


It seems that you have some problems with intellect. The function parameter is declared as int that is signed int. Nothing prevents to enter a negative number by the user. And what will you do?!
Last edited on
Vlad is right, there should be code to deal with those values.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>

// A recursive function is a function calling itself
// A basecase is an ending point for the function

using namespace std;

int factorialFinder(int x);

int main(){

    unsigned int num;

    cout << "Enter a number: ";
    cin >> num;

    cout << "Factoral is " << factorialFinder(num) << "\n";

    return 0;
}

int factorialFinder(int x){
    if (x == 1){
        return 1; // basecase
    }else{
        return (x * factorialFinder (x - 1));
    }

}



Seems to be working now ^_^
What will be the result if the user eneters 0?
Last edited on
It might need some more arrangements but for now its good to go.
Last edited on
change
1
2
if (x == 1){
        return 1;

to
1
2
if (x <= 1) {
  return x;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

#include <iostream>

// A recursive function is a function calling itself
// A basecase is an ending point for the function

using namespace std;

int factorialFinder(int x);

int main(){

    unsigned int num;

    cout << "Enter a number: ";
    cin >> num;

    cout << "Factoral is " << factorialFinder(num) << "\n";

    return 0;
}

int factorialFinder(int x){
    if (x <= 1){
        return x; // basecase
    }else{
        return (x * factorialFinder (x - 1));
    }

}



Thanks!
It is an invalid code. Factorial of 0 is equal to 1.
Last edited on
is it? i didnt know that, sorry

return 1;
hmm.... I am not a fan of flaming others but

It seems that you have
some problems with
intellect.

I should rather say that.In my earlier post i have advised that he should change it to unsigned int(or as i said do a check for negative values) and as the lines he had asked me for i used unsigned int , so he had used it in his program.
Last edited on
unsigned int num; doesn't prevent the user from entering a negative integer.
Try running your code with an input of -6.

Use an int instead of an unsigned int and check if it is negative. Also check if the number is too big to handle.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

int factorialFinder( int x );

int main()
{
    const int MAX = 13 ; // largest number we can handle (implementation dependent)
    int num;
    std::cout << "Enter a number in : ";
    std::cin >> num;

    if( num < 0 ) std::cout << "factorial of a negative number is not defined\n" ;
    else if( num > MAX ) std::cout << "the factorial of " << num << " is too big\n" ;
    else std::cout << "Factoral is " << factorialFinder(num) << '\n' ;
}

int factorialFinder( int x )
{
    if( x==0 ) return 1 ;
    else return x * factorialFinder( x-1 ) ;
}


EDIT: Hadn't seen the post by amhndu.
To clarify: 'your code' in 'Try running your code' referred to the code DanielPatrick had posted.

amhndu's suggestion of 'check for negative values' is the sensible suggestion among the lot that were posted.
Last edited on
Topic archived. No new replies allowed.