Jumat, 15 April 2011

Program C++ Fungsi Menghitung Luas Lingkaran


Algoritma :
Deskripsi
r                 : integer
luas              : float

Deklarasi
luas ß 3.14*r*r
write (luas)

Berikut programnya :

#include<iostream.h>
#include<conio.h>

void lingkaran()
{
  int r;
  cout<<"masukan nilai jari-jari : ";
  cin>>r;
  cout<<"Luas lingkaran = "<< 3.14*r*r<<endl;
}

int main() {

    lingkaran();
    return 0;
}

Program classnya :

#include <iostream.h>
#include <conio.h>

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

public:
luas();
void hitung_luasnya(){ luas = 3.14*r*r;}

private:
    int r;
    float luas;
};

luas::luas(){
cout << "Program mencari luas lingkaran \n" << endl;
}
istream& operator >>(istream& cin, luas& masukan){
   cout << "Masukkan jari-jari : ";   cin >> masukan.r;
   cout<<endl;

return cin;
}
ostream& operator << (ostream& out, const luas& keluaran)
{
  out<< " Nilai jari- jari : " <<keluaran.r<<endl;
  out<< " Luas lingkaran = "<<keluaran.luas<<endl;

return out;
}

void main(){
  luas lingkaran;
  cin>>lingkaran;
  lingkaran.hitung_luasnya();
  cout<<lingkaran;

  getch();

}


Teman-teman juga bisa mendemokannya ke jeliot ,, check it out ....
Demo ke jeliot :
Fungsi Menghitung Luas Lingkaran

import jeliot.io.*;

public class luas {
public static void main()
{
     int r ;
     float luas;
     System.out.print("Masukan jari-jari : ");
     r = Input.readInt();
    
     luas = (float)3.14*r*r;
     System.out.print("Luas lingkaran = ");
     System.out.print(luas);
 }
}

Explanation:
The above program is a program to calculate the area of ​​a circle by using the function. Which program would invoke the functionality. In this program using a variable r and spacious. The program will do the counting process with a predetermined formula. After users input the value of r the area of ​​a circle will appear.

Program C++ menghitung jarak titik A(x1,y1) dan B(x2,y2)


Algoritma :
Deskripsi
AB               : float
x1,x2,y1,y2     : float

Deklarasi
AB = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
write (a,b)

Berikut programnya :

#include <iostream.h>
#include <conio.h>
#include <math.h>

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

public:
jarak();
void hitung(){
     AB = sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
};
private:
    float x1,y1,x2,y2;
    float AB;
};

jarak::jarak(){
cout << "mencari jarak antara titik A(x1,y1) dan B(x2,y2)\n" << endl;
}
istream& operator >>(istream& cin, jarak& masukan){
   cout << "Nilai titik A " <<endl;
   cout << "Masukkan nilai x1 : ";   cin >> masukan.x1;
   cout << "Masukkan nilai y1 : ";   cin >> masukan.y1;
   cout << "Nilai titik B " <<endl;
   cout << "Masukkan nilai x2 : ";   cin >> masukan.x2;
   cout << "Masukkan nilai y2 : ";   cin >> masukan.y2;
   cout<<endl;

return cin;
}
ostream& operator << (ostream& out, const jarak& keluaran)
{
  out<< "jarak antara titik tersebut adalah : " <<keluaran.AB<<endl;


return out;
}
void main(){
jarak titik;
cin>>titik;
titik.hitung();
cout<<titik;

getch();
}


Explanation:
In this program just like the program to find the midpoint of the program but find the distance between two points is point A (x1, y1) and point B (x2, y2). Here also using variable x1, x2, y1 and y2 but the difference only variable AB. To find the distance of two points by using the formula AB = sqrt ((x1-x2) ² + (y1-y2) ²). After getting input from the user initializes the program will work with the formula specified in the program.

Program C++ mencari bilangan terbesar dari 2 bilangan yang diinputkan


Algoritma :
Deskripsi
a,b      : integer

Deklarasi
    if ( a > b )
       then
              write(a)
else
then
              write(b)
endif

Berikut programnya :

#include <iostream.h>
#include <conio.h>

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

public:
terbesar();
void bandingkan(){
     if (a > b)
        cout<< " Bilangan terbesar : "<<a;
     else
        cout<< " Bilangan terbesar : "<<b<<endl;
};
private:
     int a,b;
};

terbesar::terbesar(){
cout << "menentukan bilangan terbesar dari yang diinputkan\n" << endl;
}

istream& operator >>(istream& cin, terbesar& masukan){
     cout<<"masukan bilangan pertama : ";
     cin>>masukan.a;
     cout<<"masukan bilangan kedua : ";
     cin>>masukan.b;
     cout<<endl;

return cin;
}

ostream& operator << (ostream& out, const terbesar& keluaran){
     out<< " inilah yang terbesar "<<endl;

return out;
}

void main(){
terbesar bilangan;
cin>>bilangan;
bilangan.bandingkan();
cout<<bilangan;

getch();
}
Explanation:
At this time I wanted to try the program to find the largest number of 2 numbers are entered. Which of these programs use the election if ... else that is a> b. Seletah user input values ​​a and b values​​, the program will work by using the election to seek the greatest value of these two numbers are entered.

Template by:

Free Blog Templates