#include <iostream>
using namespace std;
int iteratif (int suku, int a, int b, int c)
{
a=0, b=1;
if (suku == 1) return b;
if (suku == 0) return a;
else{
for(int i=2; i<=suku; i++){
c = a + b;
a = b;
b = c;
}
return c;
}
}
int main()
{
int suku, a, b,c;
cout<<"Masukkan nilai suku ke-: ";
cin>>suku;
cout<<"\nBilangan fibonaccinya untuk "<<suku<<" adalah ";
cout<< iteratif ( suku, a, b, c);
return 0;
}
2. Buatlah fungsi fibonacci dengan 2 cara rekursif yang lain.
#include <iostream>
using namespace std;
int nextTerm(int n){
int firstTerm = 0, secondTerm = 1;
int nextTerm;
cout << "Fibonacci Series: " << firstTerm << " " << secondTerm << " ";
for (int i = 1; i <= n-2; ++i) {
nextTerm = firstTerm + secondTerm;
cout<<nextTerm << " ";
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
int main() {
int n;
cout << "Enter number of terms: ";
cin >> n;
nextTerm(n);
return 0;
}
3. Algoritma perkalian dengan cara penjumlahan pada algoritma 5.3. belum sempurna
karena belum mencakup semua kemungkinan, misalnya untuk harga b negatif. Buatlah.
#include <iostream>
#include <math.h>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int a,b,i,jumlah=0;
cout<<"Menghitung Perkalian dengan Penjumlahan"<<endl;
cout<<"Masukan Nilai pertama = ";cin>>a;
cout<<"Masukan Nilai Kedua = ";cin>>b;
if (a>0 && a<0 || b>0){
for(i=1;i<=b;i++)
jumlah+=a;
cout<<a<<"x"<<b<<"="<<jumlah;
}else if(b<0 && a<0){
for(i=0;i>a;i--)
jumlah-=b;
cout<<a<<"x"<<b<<"="<<jumlah;
}else{
for(i=1;i<=a;i++)
jumlah+=b;
cout<<a<<"x"<<b<<"="<<jumlah;
}
return 0;
}
4. Buatlah algoritma rekursif dari algoritma 5.3.
#include <iostream>
#include<conio.h>
#include<math.h>
using namespace std;
class hitung
{
public:
void input();
int proses();
private:
int a;
float bil,hasil,total;
};
void hitung::input(){
cin>>a;
cout<<endl;}
int hitung::proses(){
hasil=0;
total=0;
bil=-1;
for(int j=1; j<=a; j++){
bil=(bil*(-1));
total=bil/j;
hasil+=total;
if(j==1)
cout<<"("<<bil<<"/"<<j<<")";
if(j>1)
cout<<" +("<<bil<<"/"<<j<<")";
}
cout<<endl<<endl<<"hasil penjumlahan deret = "<<hasil;
return hasil;
}
int main()
{
cout<<"menghitung jumlah Hasil Deret 1-(1/2)+(1/3)-(1/4)+...+(1/n)"<<endl<<endl;
cout<<"masukan nilai n : ";
hitung deret;
deret.input();
deret.proses();
getch();
return 0;
}
5. Diberikan suatu bilangan bulat positif. Cetaklah bilangan bulat tersebut secara terbalik,
secara iteratif maupun rekursif.
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
/* Algoritma 5
1. Deklarasi
hasil,n,k : integer
proses[2],yesno[2]; character
2. Deskripsi
Read(n,k)
if(n<k)
then write("Nilai n tidak boleh lebih kecil dari k")
else
write"(Pilih P (permutasi) atau C (kombinasi))"
if(proses[0]=='p' || proses[0]=='P')
hasil = faktorial(n)/faktorial(n-k);
then write("\n \n")
then write(n,"P",k," : ",hasil)
else if (proses[0]=='c' || proses[0]=='C')
hasil = faktorial(n)/(faktorial(k) * faktorial(n-k));
then write("\n \n")
then write(n,"C",k," : ",hasil)
else
then write("\n\tAnda tidak memilih P atau C")
then write("\n\tIngin mengulang? <y/n> ")
Read(yesno[0])
if (yesno[0]=='Y'||yesno[0]=='y')
Y=1
else if (yesno[0]=='N'||yesno[0]=='n'||yesno[0]!='Y'||yesno[0]!='y')
Y=0
Kelompok 4=1500018161*/
int faktorial(int N);
int main(void)
{
int hasil;
int n,k, Y=1;
char proses[2];
char yesno[2];
while(Y) {
cout<<"\tMasukkan nilai n : ";
cin>>n;
cout<<"\tMasukkan nilai k : ";
cin>>k;
if(n<k){
cout<<"\tNilai n tidak boleh lebih kecil dari k\n";
}
else
{
cout<<"\tPilih P (permutasi) atau C (kombinasi) : ";
cin>>proses[0];
if (proses[0]=='p' || proses[0]=='P')
{
hasil = faktorial(n)/faktorial(n-k);
cout<<"\n \n";
cout<<n<<"P"<<k<<" : "<<hasil;
}
else if (proses[0]=='c' || proses[0]=='C')
{
hasil = faktorial(n)/(faktorial(k) * faktorial(n-k));
cout<<"\n \n";
cout<<n<<"C"<<k<<" : "<<hasil;
}
else
cout<<"\n\tAnda tidak memilih P atau C";
cout<<"\n\tIngin mengulang? <y/n> ";
cin>>yesno[0];
if (yesno[0]=='Y'||yesno[0]=='y')
Y=1;
else if (yesno[0]=='N'||yesno[0]=='n'||yesno[0]!='Y'||yesno[0]!='y')
Y=0;
}
}
}
int faktorial(int N)
{
int F;
if (N<=1)
{
return(1);
}
else
{
F = N * faktorial(N-1);
return(F);
}
}
6. Buatlah suatu subprogram yang mengembalikan nilai maksimum pertama (misal m1) dan
nilai maksimum kedua (misal m2, dan m1 m2) dari array dengan n bilangan bulat.
#include <iostream>
using namespace std;
int main()
{
int max,n,i,min;
float A[100];
cout <<"Masukkan Jumlah Data : ";cin>>n;
for(i=0;i<n;i++){
cout << "masukkan bilangan ke "<< i+1 << " : ";
cin >> A[i];}
max = A[0];
for (i=1;i<n;i++){
if (max < A [i])
max = A[i];
}
cout <<"Nilai Terbesar adalah : "<<max<<endl;
return 0;
}
7. Algoritma 5.5 menggunakan fungsi untuk menghitung n faktorial secara rekursif. Buatlah
algoritma menghitung n faktorial dengan menggunakan prosedur !
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(void) {
int n;
int faktorial (int k);
printf("Masukkan sembarang bilangan positif : ");
scanf("%d",&n);
printf("\nMelalui perhitungan rekursif, %d! = %d\n",n,faktorial(n));
system("pause");
return 0;
}
int faktorial (int k){
if (k==0)
return 1;
else
return k*faktorial(k-1);
}
9. [Proyek] Buatlah suatu class yang akan mengeksplorasi kalimat. Kalimat dapat disimpan
dalam bentuk array (kumpulan) karakter.
a. masukan user diterima huruf demi huruf sampai diakhiri tanda akhir baris
(sentinel)
b. buat method untuk menghitung statistik :
i. huruf hidup (vokal)
ii. huruf mati (konsonan)
iii. banyak kata
iv. huruf terbanyak dalam kalimat.
c. buat method untuk mengubah huruf awal setiap kata menjadi huruf besar
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
class septiankonsvok{
private:
int i;
int vokal;
int konsonan;
int x;
char kalimat[20];
public:
septiankonsvok();
void input();
void proses();
void output();
};
septiankonsvok::septiankonsvok(){
cout<<"\t\tPEROGRAM MENGHITUNG HURUF KONSONAN DAN VOKAL\n";
cout<<"\t\t--------------------------------------------\n";
cout<<endl;
cout<<"Nama : septian eka"<<endl;
cout<<"Prodi :teknik informatika"<<endl;
cout<<endl<<endl;
}
void septiankonsvok::input(){
cout<<"::.silahkan masukkan kalimat.::\n";
cin.getline(kalimat,20);
}
void septiankonsvok::proses(){
i=0,vokal=0,konsonan=0;
x=strlen(kalimat);
for(i=0;i<x;i++)
{
if(kalimat[i]=='a'||kalimat[i]=='u'||kalimat[i]=='e'||kalimat[i]=='o')
vokal++;
konsonan++;
}
}
void septiankonsvok ::output(){
cout<<"Jumlah huruf vokal\t:"<<vokal<<"huruf"<<endl;
cout<<"Jumlah huruf konsonan\t"<<konsonan<<"huruf"<<endl;
}
int main()
{
septiankonsvok skv;
skv.input();
skv.proses();
skv.output();
return 0;
}
Sign up here with your email
ConversionConversion EmoticonEmoticon