Jawab:
#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;
}
output
Kasus 5.2. Cetaklah bilangan ganjil dari 0 sampai 10 menggunakan perulangan (for, while – do,
Jawab:
#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;
}
output
Kasus 5.3. Carilah rata-rata dari n bilangan bulat positif.
Jawab:
#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;
}
output
Kasus 5.4. Hitunglah rata-rata dari bilangan bulat positif, di mana banyak data ditentukan dari data yang dimasukkan
Jawab:
#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;
}
output
Kasus 5.5. Tentukan nilai dari :
1Jawab:
1 1 1 1 1 23 n i ni ¦ sampai selisih 2 suku tidak lebih dari 0.001. Tentukan pula nilai n terakhir.
#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;
}
output
Kasus 5.6. Carilah nilai dari n! ( n faktorial).
Jawab:
#include <iostream>
using namespace std;int main(int argc, char** argv) {
long desimal, pembagi, bit;
pembagi=1073741824;
cout<<"\t\t Program Konversi Bilangan Desimal ke Biner"<<endl<<endl;
cout<<" Masukan bilangan desimal : ";
cin>>desimal;
while(pembagi>desimal)pembagi/=2;
do{
bit=desimal/pembagi;
cout<<bit;
desimal=desimal%pembagi;
pembagi/=2;
}
while(pembagi>=1);
cout<<"\n";
return 0;
}
output