Jumat, 15 April 2011

Program C++ Fungsi Menghitung Volume Bola


Algoritma :
Deskripsi
r                 : integer
volume           : float

Deklarasi
volume <= 4/3*3.14*r*r*r
write (volume)

Berikut programnya :

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

void bola()
{
  int r,volume;
  cout<<"masukan nilai jari-jari : ";
  cin>>r;
  cout<<"volume bola = "<<4/3*3.14*r*r*r<<endl;
}

int main() {

    bola();
    return 0;
}

Program classnya:

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

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

public:
volume();
void hitung_volumenya(){ volume = 4/3*3.14*r*r*r;}

private:
    int r;
    float volume;
};

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

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

return out;
}

void main(){
  volume bola;
  cin>>bola;
  bola.hitung_volumenya();
  cout<<bola;

  getch();

}

Teman-teman juga bisa mendemokannya ke jeliot ,, check it out ....
Demo ke jeliot :
Fungsi Menghitung Volume Bola
import jeliot.io.*;

public class volume {
public static void main()
{
     int r ;
     float volume;
     System.out.print("Masukan jari-jari : ");
     r = Input.readInt();
    
     volume = (float)4/3*(float)3.14*r*r*r;
     System.out.print("Volume bola = ");
     System.out.print(volume);
 }
}
Explanation:
The above program is a program to calculate the volume of the ball by using the function. Which program would invoke the functionality. In this program using a variable r and the volume. The program will do the counting process with a predetermined formula. After users input the value of r then the volume of the ball will appear.

0 komentar:

Posting Komentar

Template by:

Free Blog Templates