Selasa, 12 April 2016

diktat bab 5
5.1
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int interatif (int x){
int n, c, first = 0, second = 1, next;
cout <<"Masukan Nilai =";cin>>n;cout<<endl;
    cout <<"first =" <<n<< " fibonacci:- " <<endl;
for (c=0;c < n ; c++ )
   {
      if (c<=1)
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
         cout<<next<<endl;
   }
}
int main(int argc, char** argv) {
   int x;
   interatif(x);

 
return 0;
}

5.2
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int interatif (int x){
int c,n,first = 0, second = 1, next;
cout <<"Masukan Nilai =";cin>>n;cout<<endl;

for (c=0;c < n ; c++ )
   {
      if (c<=1)
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
         cout<<next<<endl;
   }
} int rekursif (int x){
int n,next,first=0,second=1;
cout <<"\nMasukan Nilai =";cin>>n;cout<<endl;

for (x=0;x<n;x++){
if (x<=1)
next=x;
else{
next = first + second;
first=second;
second=next;
}
cout<<next<<endl;
}
}

int main(int argc, char** argv) {
   int x;
   cout<<"Interatif ="<<interatif(x);cout<<endl;
   cout<<"Recurtif  ="<<rekursif (x);cout<<endl;

 
return 0;
}

5.3
#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;
}

5.4
#include <iostream>
#include <math.h>
using namespace std;
int rekursif (int x){
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;
}
}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
int x;
rekursif(x);
return 0;
}

5.5
#include <iostream>
#include <conio.h>
using namespace std;
int iteratif(int x){
for(int i=10; i>=0; i--){
cout<<i<<endl;;
}
}
int rekursif(int x){
x=10;
int i;
if(x>=0){
rekursif(x-1);
i--;
cout<<x;
}
}
int main(){
int x;
cout<<"\nIteratif : ";
iteratif(x);
cout<<"\nRekursif : ";
rekursif(x);
getch();
return 0;
}

5.8
#include <iostream>
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 x,y,i;
cout<<"Kasir Supermarket"<<endl;
cout<<"Masukan Seberapa Banyak ";
for (i=0;i<=x;i++)
return 0;
}

latihan
1.#include <iostream>
using namespace std;
int iteratif (int suku, int a, int b, int c)  { a=0, b=1; 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.
#include <stdio.h>
#include <conio.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int fibonacci(int n);
void main()
{
int bil,urfib,ulangi;
do
{
clrscr();
printf("Bilangan fibonacci urutan ke? ");
scanf("%d",&bil);
urfib=fibonacci(bil);
printf("\nUrutan fibonacci ke %d, adalah %d",bil,urfib);
printf("\n\nApakah anda ingin mengulangi?");
printf("\n1. YA");
printf("\n2. TIDAK (keluar) ");
scanf("%d",&ulangi);
}while(ulangi==1);
}
int fibonacci(int n)
{
int fibonac;
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
fibonac=fibonacci(n-1)+fibonacci(n-2);
return (fibonac);

3.
#include <cstdlib>
#include <iostream>
using namespace std;
class bilangan{
friend ostream& operator<<(ostream&, bilangan&);
friend istream& operator>>(istream&, bilangan&);
public:
bilangan(){};
void proses(){
i =0;
while (!(i>100))
{ if (i % 3==1 && i % 5==1){
}
else
{
cout << i << endl; }
i =i+15;
}
}
private:
int i;
};
istream& operator>>(istream& in, bilangan& masukkan){
cout<<"Bilangan Yang Habis Dibagi 3 & 5 = "<<endl;
in >>masukkan.i;
return in;
}
ostream& operator<<(ostream& out, bilangan& keluaran){
cout<<"Bilangannya adalah "<<keluaran.i<<endl;
return out;
}
int main(int argc, char *argv[])
{
bilangan bil;
cin >> bil;
bil.proses();
cout<< bil;
cout<< endl;
system("PAUSE");
return 0;
}

Tidak ada komentar:

Posting Komentar