fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. bool isPrime(int num) {
  5. for(int i = 2; i < num; i++)
  6. if(num%i == 0)
  7. return false;
  8. cout << "TES" << endl;
  9. return true;
  10. }
  11. int tes(int &angka) {
  12. angka += 1;
  13. cout << "Dalam fungsi: " << angka << endl;
  14. return 0;
  15. }
  16.  
  17. int jumlah(int batas) {
  18. if(batas == 0)
  19. return 0;
  20. return batas + jumlah(batas-1);
  21. // jumlah(10) = 10+9+8+7+6+5+4+3+2+1+0
  22. // jumlah(9) = 9+8+7+6+5+4+3+2+1+0
  23. // ...
  24. // jumlah(1) = 1+0
  25. // jumlah(0) = 0
  26. }
  27.  
  28. int main() {
  29. /*
  30. // Refresher percabangan perulangan
  31. int angka1, angka2;
  32. angka1 = 10;
  33. angka2 = 0;
  34. for(int i = 0; i < angka1; i++) {
  35. if(i == 5)
  36. continue;
  37. angka2 += i;
  38. }
  39. cout << angka2 << endl;
  40. // 0+1+2+3+4+6+7+8+9 = 40
  41.  
  42. // Array
  43. int array[10]; // 0--9
  44. array[0] = 100;
  45. array[1] = 150;
  46. for(int i = 0; i < 10; i++) {
  47. array[i] = 0;
  48. }
  49. int angka1, angka2;
  50. angka1 = 20;
  51. angka2 = angka1;
  52.  
  53. int arr1[10], arr2[10];
  54. arr1[0] = 2;
  55. arr1[1] = 3;
  56. // arr2 = arr1; // ERROR
  57. for(int i = 0; i < 10; i++)
  58. arr2[i] = arr1[i];
  59. // Fungsi & Rekursi
  60. if(isPrime(37))
  61. cout << "37 = PRIMA" << endl;
  62. else
  63. cout << "37 = KOMPOSIT" << endl;
  64.  
  65. if(isPrime(36))
  66. cout << "36 = PRIMA" << endl;
  67. else
  68. cout << "36 = KOMPOSIT" << endl;
  69. */
  70. /*
  71. int num = 10;
  72. tes(num);
  73. cout << "Luar fungsi: " << num << endl;
  74. // pass-by-value
  75. // pass-by-reference
  76. */
  77. cout << jumlah(10) << endl;
  78. return 0;
  79. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
55