1.mencari bilangan terbesar dari 3 bilangan bulat
#include <cstdlib>
#include <iostream>
using namespace std;
class Banding{
friend istream& operator>>(istream&,Banding&);
public:
Banding(){};
void bandingkan(){
int maks=x;
if(y>maks)maks=y;
if(z>maks)maks=z;
cout<<"bilangan terbesar:"<<maks;
}
private:
int x,y,z;
};
istream& operator>>(istream& in,Banding& bilangan){
cout<<"bilangan pertama=";
in>>bilangan.x;
cout<<"bilangan kedua=";
in>>bilangan.y;
cout<<"bilangan ketiga=";
cin>>bilangan.z;
return in;
};
int main(int argc, char *argv[])
{
Banding bilangan;
cin>>bilangan;
bilangan.bandingkan();
system("PAUSE");
return EXIT_SUCCESS;
}
2.fungsi menukar nilai a ke b dan b ke a
#include <cstdlib>#include <iostream>
using namespace std;
void tukar(int *a,int *b)
{ int temp;
temp=*a;
*a=*b;
*b=temp;
}
int main(int argc, char *argv[])
{
int a=3,b=5;
cout<<"sebelum tukar\n";
cout<<"isi nilai1="<<a<<endl;
cout<<"isi nilai2="<<b<<endl;
tukar(&a,&b);
cout<<"sesudah tukar\n";
cout<<"isi nilai1="<<a<<endl;
cout<<"isi nilai2="<<b<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
3.fungsi mengembalikan nilai
#include <iostream>
using namespace std;
int tambah5(int x)
{
return(x+5);
}
int main(int argc, char *argv[])
{
int x,y;
cout<<"masukan harga x:";cin>>x;
y=tambah5(x);
cout<<"setelah masuk fungsi bernilai:"<<y;
system("PAUSE");
return EXIT_SUCCESS;
}
4.fungsi tak mengembalikan nilai
#include <cstdlib>
#include <iostream>
using namespace std;
void cetak(int n)
{
int i;
for(i=1;i<=n;i++)
cout<<"harga n="<<i<<endl;
}
int main(int argc, char *argv[])
{
int n;
cout<<"masukan harga n:";cin>>n;
cetak(n);
system("PAUSE");
return EXIT_SUCCESS;
}
5.fibonacci
#include <cstdlib>
#include <iostream>
using namespace std;
int fibonacci (int n){
if((n==1)||(n==2))return(1);
else return(fibonacci(n-1)+fibonacci(n-2));
}
int main(int argc, char *argv[])
{
int i,n;
cout<<"sampai suku ke:";cin>>n;
for(i=1;i<=n;i++)cout<<fibonacci(i)<<"";
system("PAUSE");
return EXIT_SUCCESS;
}
6.fungsi iteratif
#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;
void balik(char *s)
{
int i;
for(i=strlen(s)-1;i>=0;i--)
cout<<s[i];
}
int main(int argc, char *argv[])
{
char *kata="algoritma";
balik(kata);
system("PAUSE");
return EXIT_SUCCESS;
}
7.fungsi rekursif
#include <iostream>
#include <string.h>
using namespace std;
void balik(char *s)
{
if(*s !='\0'){
balik(&s[1]);
cout<<s[0];
}
}
int main(int argc, char *argv[])
{
char *kata="algoritma";
balik(kata);
system("PAUSE");
return EXIT_SUCCESS;
}
8.class versi iteratif
#include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
class GCD{
friend ostream& operator<<(ostream&, GCD&);
friend istream& operator>>(istream&, GCD&);
public:
int HitungGCD(int,int);
private:
int x,y;
};
int GCD::HitungGCD(int c,int d)
{ int r;
while(d>0){
r=c%d;
c=d;
d=r;
}
return (c);
}
istream& operator>>(istream& in,GCD& a){
cout <<"masukan bilangan pertama:";
in>>a.x;
cout<<"masukan bilangan kedua:";
in>>a.y;
return in;
}
ostream& operator<<(ostream& out,GCD& a){
out<<"Ggcd("<<a.x<<","<<a.y<<")=";
out<<a.HitungGCD(a.x,a.y);
return out;
}
int main(int argc, char *argv[])
{
GCD run;
cin>>run;
cout<<run;
system("PAUSE");
return EXIT_SUCCESS;
}
9.fungsi template
#include <cstdlib>
#include <iostream>
using namespace std;
template<class T>
T tambah5(T x){
return(x+5);
}
int main(int argc, char *argv[])
{
int x;
float y;
cout<<"masukan harga x(integer):";cin>>x;
cout<<"stelah masuk fungsi bernilai:"<<tambah5(x)<<endl;
cout<<"masukan harga x(float):";cin>>y;
cout<<"setelah masuk fungsi bernilai:"<<tambah5(y);
system("PAUSE");
return EXIT_SUCCESS;
}
No comments:
Post a Comment