• We need your support!

    We are currently struggling to cover the operational costs of Xtremepapers, as a result we might have to shut this website down. Please donate if we have helped you and help make a difference in other students' lives!
    Click here to Donate Now (View Announcement)

need help with visual c++. computing 9691

Messages
15
Reaction score
0
Points
0
this refers to the following question:"You are required to design a complete system which will allow you to calculate the factorial of n, a value which is specified by the user."
SOL:
//Programme to output factorials
#include <iostream>
using namespace std;

int main ()
{
int fct,fctval; //fct is the value whose factorial we need, and fctval is the factorial
cout<< "Please enter value:";
cin>> fct;
while (fct>0){
fctval=fct*(--fct);
--fct;
}
cout<<fctval;

return 0;
}

wenever i enter a value, the output is 1. CAn u pls tell me wat im doing wrong here?
 
Messages
15
Reaction score
0
Points
0
alright, so i asked about this in another group, and i got the following answer.
#include <iostream.h>
int factorial(int);
void main(void) {
int number;

cout << "Please enter a positive integer: ";
cin >> number;
if (number < 0)
cout << "That is not a positive integer.\n";
else
cout << number << " factorial is: " << factorial(number) << endl;

}

int factorial(int number) {
int temp;
if(number <= 1) return 1;
temp = number * factorial(number - 1);
return temp;

}
The program works fine, but Im a little confused about wat happens in the factorial function , and as soon as i get an ans ill post it:)
 
Messages
971
Reaction score
532
Points
103
It's a recursive loop. First the loop checks if the number is 0 or 1, if so it'll just return 1 because 0! and 1! are 1. Otherwise it will continue looping through the function until it multiples the number with the preceding ones. I don't know much about recursive loops either, and I would just do it using a simpler way (I work with C#, so you'll have to translate this to C++ using some online translator, sorry):

public int factorial(int input)
{
if (input == 0 || input == 1)
return input = 1; // 0! and 1! are 1
else
{
int temp = 1; // initialize temp value for loop (we don't start from 0 otherwise the output will always be 0)
for (int i = 1; i <= input; i++) // note: this loop will start from 1, and stop once it reaches the number input
temp = temp * i; // multiply iterator with temp num

return temp; // return temp number
}
}

I hope this helped.
 
Messages
15
Reaction score
0
Points
0
thx a lot i get the recursion part, but in the line temp=number*factorial(number-1) ( from my program), i cant understand the reason for "factorial". Wat exactly is that, i.e. a variable/operator?:s
 
Messages
971
Reaction score
532
Points
103
It's a function. You basically called it from your main routine and it will call itself again in the function for recursion. It calculates the factorial using the formula n! = n(n-1)! (which is why I personally found it confusing at first, it would be easier writing an algorithm using n x (n-1) x (n-2) etc.

Let's say you input the number 4. You would expect it to do 4(4-1)! Here is what the code does:

factorial(4) = 4 * factorial(4)
= 4 * 3 * factorial(3)
= 4 * 3 * 2 * factorial(2)
= 4 * 3 * 2 * 1 * factorial(1)

The above is what the line "temp = number * factorial(number - 1);" does. It calls the function again, but with (n-1). If you find it confusing, then I suggest not using it since there might be easier ways to solve the problem. Syntactically, the code I wrote above and the one you have in C++ do the same thing.

I hope this explanation isn't confusing.. :p
 
Top