Below is the program for the factorial function recursively
search n! ...
The program is as follows:
# include <iostream>
float factorial (float n)
{
if (n> 1)
return n * factorial (n-1);
else
return 1;
}
main ()
{
float fac, n;
court <<"\ t \ tMENCARI factorial \ n";
court <<"input number ="; cin>> n;
court <<"\ nEnter a number: \ n";
cin>> n;
court <<"Faktorialdari" <<n <<"is" <<factorial (n) <<endl;
return 0;
}
The above program consists of only 2 functions:
- factorial (float n)
- main ()
The program above using the user input, ie the values in the fed itself by the user.
In the fourth row (n> 1) a statement of function, check if n is greater than 1. If yes, then the factorial function will memangil itself with the argument n-1. This will be done continuously for n greater than 1. Once n reaches 1, then the condition if (n> 1) This is false and the factorial function returns the value 1.
search n! ...
The program is as follows:
# include <iostream>
float factorial (float n)
{
if (n> 1)
return n * factorial (n-1);
else
return 1;
}
main ()
{
float fac, n;
court <<"\ t \ tMENCARI factorial \ n";
court <<"input number ="; cin>> n;
court <<"\ nEnter a number: \ n";
cin>> n;
court <<"Faktorialdari" <<n <<"is" <<factorial (n) <<endl;
return 0;
}
The above program consists of only 2 functions:
- factorial (float n)
- main ()
The program above using the user input, ie the values in the fed itself by the user.
In the fourth row (n> 1) a statement of function, check if n is greater than 1. If yes, then the factorial function will memangil itself with the argument n-1. This will be done continuously for n greater than 1. Once n reaches 1, then the condition if (n> 1) This is false and the factorial function returns the value 1.
Tidak ada komentar:
Posting Komentar