Factorial in C language
Factorial is the natural number n is the product of multiplication between positive integers less than or equal to n.
Factorial is written with the symbol "!"
Example: 4! = 4 x 3 x 2 x 1
3! = 3 x 2 x 1
Now we will make it in code
first in the itterative form
#include <stdio.h> // for standard input output
int main ()
{
int factorial,f jml;
scanf ("% d", & factorial);
jml = 1;
if (factorial == 1) printf ("% d", factorial);
for (f = 1; f <= factorial; f ++)
{
jml = jml * f;
}
printf ("% d \ n", jml);
return 0;
}
next we will make it recursive
#including <stdio.h>
int fact (int factorial)
{
if (factorial == 1) return factorial;
else returns factorial * fact (factorial - 1);
}
int main ()
{
int factorial,f, jml;
scanf ("% d", & factorial);
jml = fact (factorial);
printf ("% d \ n", jml);
return 0;
}
That is all from me now
thank you
Factorial is written with the symbol "!"
Example: 4! = 4 x 3 x 2 x 1
3! = 3 x 2 x 1
Now we will make it in code
first in the itterative form
#include <stdio.h> // for standard input output
int main ()
{
int factorial,f jml;
scanf ("% d", & factorial);
jml = 1;
if (factorial == 1) printf ("% d", factorial);
for (f = 1; f <= factorial; f ++)
{
jml = jml * f;
}
printf ("% d \ n", jml);
return 0;
}
next we will make it recursive
#including <stdio.h>
int fact (int factorial)
{
if (factorial == 1) return factorial;
else returns factorial * fact (factorial - 1);
}
int main ()
{
int factorial,f, jml;
scanf ("% d", & factorial);
jml = fact (factorial);
printf ("% d \ n", jml);
return 0;
}
That is all from me now
thank you
Comments
Post a Comment