Rabu, 27 April 2011

Menghitung Deret Bilangan dengan Fungsi Rekrusif

Teman-teman bagai mana cara menghitung deret bilangan becahan, mau tau ney programnya...

#include <iostream.h>

class hitung
    {
    public:
     int proses();
     void input();
    private:
     int n;
     float rumus,jumlah,total;
    };

    void hitung::input()
    {
     cin>>n;
     cout<<endl;
    }
    int hitung::proses()
    {
     jumlah=0;
     total=0;
     rumus=-1;

     for(int j=1; j<=n; j++)
     {
     rumus=(rumus*(-1));
     total=rumus/j;
     jumlah+=total;
     if(j==1)
     cout<<"("<<total<<")";
     if(j>1)
     cout<<"+("<<total<<")";
     }
    cout<<endl<<endl<<"hasil penjumlahan deret = "<<jumlah;
    return jumlah;
    }

    int main()
    {
    cout<<"program sederhana menghitung jumlah dari rumus 1-(1/2)+(1/3)-(1/4)+...+(1/n)"<<endl<<endl;
    cout<<"tentukan nilai n : ";
    hitung deret;
    deret.input();
    deret.proses();
   
    return 0;
    }
stelah di compile akan terlihat seperti ini...

Ada versi java Eliotnya juga loch...
ne programnya:

import jeliot.io.*;

public class hitung {
 
    public void input(){
    System.out.println("tentukan nilai n:");
    n=Input.readInt();
    }
    public void proses(){
           jumlah=0;
           total=0;
           rumus=-1;
    for(int j=1; j<=n; j++)
     {
     rumus=(rumus*(-1));
     total=rumus/j;
     jumlah+=total;
     if(j==1)
     System.out.println(+total);
     if(j>1)
     System.out.println(+total);
    
     System.out.println("hasil penjumlahan deret = "+jumlah);
     }
     }
private int n;
private float rumus,jumlah,total;

    public static void main() {
       hitung h=new hitung();
       h.input();
       h.proses();

    }
}

Selamat mencoba ya,moga sukses ^_^
Share This :

Related Templates

Tidak ada komentar:

Posting Komentar

 
Copyright © 2011. Informatika Creation - RYA