CONTOH PROGRAM MENJUMLAHKAN
FACTORIAL(DENGAN DAN TANPA REKURSI FUNGSI) DALAM DEV C++
FACTORIAL
DENGAN REKURSI FUNGSI
PROGRAMNYA:
#include <cstdlib>
#include <iostream>
// yogi.king.pratama@blogspot.com
using namespace std;
int factorial (int a)
{
if
(a>1)
return (a * factorial (a-1));
else
return (1);
}
int main ()
{
int
n;
cout
<< "N: ";cin >> n;
cout
<< n << " != " << factorial (n)<<endl;
system("pause");
return
0;
}
FACTORIAL
TANPA REKURSI FUNGSI
PROGRAMNYA:
#include <cstdlib>
#include <iostream>
using namespace std;
void yogip();
void yogip()
{
int
i,w,c,t,l,n,m,fak,fak1,fak2;
cout<<"n!:";cin>>n;
fak1=1;
for
(i=1;i<=n;i++)
fak1=fak1*i;
cout<<"n!:"<<fak1<<endl;
cout<<"m!:";cin>>m;
fak=1;
for
(l=1;l<=m;l++)
fak=fak*l;
cout<<"m!:"<<fak<<endl;
w=n-m;
fak2=1;
for
(t=1;t<=w;t++)
fak2=fak2*t;
cout<<"n-m!:"<<fak2<<endl;
c=fak1/(fak2*fak);
cout<<"c:"<<c<<endl;
}
int main(int argc, char *argv[])
{
system("cls");
yogip();
system("pause");
return
0;
}
HASIL
COMPILE:
Tidak ada komentar:
Posting Komentar