TUGAS 1
1. Faktorial
Algoritma
faktorial
|
Listing
program
#include<iostream.h>
|
#include<conio.h>
|
int main()
|
{
|
int m,i;
|
long int x;
|
cout<<"***
Program Faktorial ***\n\n";
|
cout<<"masukkan
bilangan: ";cin>>m;
|
x=1;
|
for (i=m;i>0;i--)
|
{
|
x=x*i;
|
}
|
cout<<m<<"! =
"<<x<<"\n";
|
getch();
|
}
|
2. Pangkat
Algoritma
pangkat
|
Listing
progarm
#include
<cstdlib>
#include <iostream>
using namespace
std;
int main(int
argc, char *argv[])
{
int nilai1,nilai2,jumlah;
cout<<"**************************************"<<endl;
cout<<"******** Menghitung pangkat ********"<<endl;
cout<<"**************************************"<<endl;
cout<<endl;
jumlah=1;
cout<<"Masukkan nilai a:
";
cin>>nilai1;
cout<<"Masukkan nilai b:
";
cin>>nilai2;
for(int a=0;a<nilai2;a++){
jumlah*=nilai1;
}
cout<<endl;
cout<<"Nilai
"<<nilai1<<" pangkat "<<nilai2<<"
adalah: "<<jumlah;
cout<<endl;
cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
3. Tukar
Angka
Algoritma tukar angka
|
Listing
program
#include
<cstdlib>
|
#include
<iostream>
|
using namespace std;
|
int main(int argc,
char *argv[])
|
{
|
int
angka_1, angka_2;
|
cout<<”Program tukar
angka”<<endl;
|
cout<<"masukkan angka 1 : ";cin>> angka_1;
|
cout<<endl;
|
cout<<"masukkan angka 2 : ";cin>> angka_2;
|
cout<<"nilai angka 1 = "<<angka_1<<endl;
|
cout<<"nilai angka 2 = "<<angka_2<<endl;
|
cout<<endl;
|
cout<<endl;
|
int
temp = angka_1;
|
angka_1 = angka_2;
|
angka_2 = temp;
|
cout<<"setelah ditukar"<<endl;
|
cout<<endl;
|
cout<<"nilai angka 1 = "<<angka_1<<endl;
|
cout<<"nilai angka 2 = "<<angka_2<<endl;
|
system("PAUSE");
|
return EXIT_SUCCESS;
|
}
|
Algoritma
sequential search
|
Listing
program
#include<iostream.h>
|
#include<conio.h>
|
class sequential{
|
int data[50];
|
int jml;
|
public:
|
void input();
|
void search();
|
};
|
int main(){
|
sequential x;
|
x.input();
|
x.search();
|
getch();
|
}
|
void sequential::input(){
|
cout<<"Masukkan banyaknya data : "; cin>>jml;
|
for (int i=0;i<jml;i++){
|
cout<<"masukkan
data ke- "<<i<<" : ";
|
cin>>data[i];
|
}
|
}
|
void sequential::search(){
|
int cari, flag=0;
|
cout<<"masukkan data yang ingin dicari : "; cin>>cari;
|
for (int j=0;j<jml;j++){
|
if(data[j]==cari) flag++;
|
}
|
if (flag!=0) cout<<"data yang ditemukan ada
"<<flag<<endl;
|
else if (flag==0) cout<<"data tidak
ditemukan"<<endl;
|
}
|