document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int jumlahGanjilIteratif(int x){
  6.     int jumlah = 0;
  7.  
  8.     for(int i=1; i<=x; i+=2){
  9.         jumlah += i;
  10.     }
  11.  
  12.     return jumlah;
  13. }
  14.  
  15. int jumlahGanjilRekursif(int x){
  16.     if(x%2 == 0){
  17.         x--;
  18.     }
  19.  
  20.     if(x < 1){
  21.         return 0;
  22.     }else{
  23.         return x + jumlahGanjilRekursif(x-2);
  24.     }
  25. }
  26.  
  27. int main(){
  28.     int n;
  29.     cout << "Masukkan n : "; cin >> n;
  30.     cout << "Jumlah ganjil dengan Iteratif = " << jumlahGanjilIteratif(n) << endl;
  31.     cout << "Jumlah ganjil dengan Rekursif = " << jumlahGanjilRekursif(n) << endl;
  32.  
  33.     return 0;
  34. }
');