Senin, 20 Juni 2011

Menghitung Nilai Faktorial menggunakan Program JAVA


a glimpse of the factorial :

Here we let (n) as the number who would we look for value faktorialnya.
In mathematics the factorial of a bialngan formulated by:

n! = n * (n-1) ... ... * 1

or

n! = 1 * (n 1) ... ..* n


ALGORITHM calculate the factorial :

1.Tentukan fariabel i, n, factorial
2.faktorial = 1;
3.scanf (n)
4.for (i = 1; i <= n; i) {factorial = factorial * i;} 5.printf (factorial); 6.End.
The following java program :

import jeliot.io.*;


public class faktorial
{
public static void main (String[ ] args)
{
long limit = 20; // menghitung faktorial integer daeri 1 – 20
long faktorial = 1; // pendefinisian variabel faktorial

for (int i = 0; i <= limit; i++)
{
faktorial = 1;

for (int faktor = 2; faktor <= i; faktor ++)
faktorial *= faktor;
System.out.println (i + "!" + " adalah " + faktorial);
}
}
}

Minggu, 19 Juni 2011

Counting the roots of quadratic equation using the function class


class in c + + function to categorize the variables, class is almost the same as the struct, the difference is the class name can be changed while in stuctr can not.

Counting the roots of quadratic equation programs use the function class
#include
#include
#include
class Persamaan{
public:
Persamaan();
void hitung();
private:
double a,b,c;
double diskriminan,x1,x2,re,im;
};
int main()
{
Persamaan x;
x.hitung();
getch();
return 0;
}
Persamaan::Persamaan(){
cout << "Persamaan Kuadrat" << endl;
cout << "a : ";

cin >> a;
cout << "b : ";

cin >> b;
cout << "c : ";

cin >> c;
}
void Persamaan::hitung (){
// hitung diskriminan
diskriminan=b*b-4*a*c;
if(diskriminan > 0)
{
x1 = (-b + sqrt(diskriminan))/(2*a);
x2 = (-b - sqrt(diskriminan))/(2*a);
cout << "Akar real : " << endl;
cout << "x1 : " << x1 << endl;
cout << "x2 : " << x2 << endl;

}
else
if(diskriminan ==0)
{
x1=-b/(2*a);
x2=x1;
cout << "Akar kembar : " << endl;

cout << "x1 : " << x1 << endl;

cout << "x2 : " << x2 << endl;

}
else //diskriminan <0
{
re= -b/(2*a);
im= sqrt(fabs(diskriminan))/(2*a);
cout << "Akar kompleks : " << endl;
cout << "x1 : " << re <<" + " << im << endl;
cout << "x2 : " << re <<" - " << im << endl;

}
}

Kamis, 16 Juni 2011

Print the prime numbers between 1-100 by function overloading


function and operator overloading are the two most important things in C + + programming language.
mekipun unique function, this function is not much different from other types of functions.
In C + +, a function call is not only determined by the function name, but also
by type and number of actual parameters.
Facilities relating to existing functionality
other in C + + is a template function, operator function, inline function.

The following is a program of Printing prime numbers between 1-100 by function overloading



#include
#include
#include
#include

class prog
{
friend istream & operator >> ( istream &, prog & );
friend ostream & operator << ( ostream &, prog & );

public:
void id();
prog();
char nama[60];
private:
float m;
float c;
float energi;

};
void prog::id()
{
cout << "Nama Program : ";

cin.getline( nama, 60 );
cout << endl;

}
prog::prog()
{

}
ostream & operator << ( ostream & out, prog & y )

{

{
int i,j,k;
cout<<"bilangan 1-100"<

for (i=2;i<=100;i++){

for (j=2;j<=i/2;j++){

k=i%j;
if (k==0 )break;
}if(k!=0)
cout<<

}
}
return out;
}
int main()
{ cout << "\t\t\t# C++ #\n\n";
char nama[90] =
{
"Cetak Bilangan Prima "
};
cout << "Nama Program :" << nama << endl;
cout << "\n";

prog a;
cout << a;

getch();
return 0;
}


Senin, 13 Juni 2011

POINTER


Pointer is a variable that contains the memory address of a
other variables.
This address is the location of other objects (usually other variables) in
memory.
For example, if a variable contains the address of another variable, the first variable
said pointing to the second variable

Pointer operators there are two, namely:
 OperatorØ &
& Operator is unary (requires only one operand only).
Operator & produces the address of the operandnya.
 Operator *Ø
* Are unary operators (requires only one operand only).
Operator * value that is at an address.



pointer declaration
Tipe_data * nama_pointer;

sample program using C + + pointers
#include
#include

using namespace std;

int main(int argc, char *argv[])
{

class node {
public :
int data;
node*berikut;
};

void main(){
//langkah satu
node*baru;
baru=new node;
baru->data=5;
baru->berikut=NULL;
cout<<"Isi data node baru adalah : "<data<


//langkah dua
node*lain;
lain=new node;
lain->data=6;
lain->berikut=NULL;
cout<<"Isi data node lain adalah : "<data<


//langkah tiga : menyambung rantai
baru->berikut=lain;
cout<<"Isi data node lain dicetak dari node baru adalah :";
cout<berikut->data<
//langkah empat
node*kepala=baru;
cout<<"mencetak node pertama dari pointer kepala :";
cout<data<
cout<<"Mencetak node kedua dari pointer kepala :";
cout<berikut->data<
//langkah lima : pointer yang jalan-jalan
cout<<"Menggunakan perulangan untuk mencetak setiap data pada rantai\n";

node*jalan=kepala;
int i = 1;
while(jalan !=NULL){
cout<<"Data ke-"<<<">"<data<
i++;
jalan=jalan->berikut;
}
//langkah enam : bukti bahwa pointer data tidak kehilangan kepala
cout<<"Mencetak node pertama dari pointer kepala : ";


cout<data<


cout<<"Mencetak node kedua dari pointer kepala : ";
cout<berikut->data<
}
system("PAUSE");
return EXIT_SUCCESS;
}