fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6. // Struktur kondisi
  7. int nilai;
  8. cout << "Masukkan nilai: ";
  9. cin >> nilai;
  10.  
  11. if (nilai > 0) {
  12. cout << "Nilai positif" << endl;
  13. } else if (nilai < 0) {
  14. cout << "Nilai negatif" << endl;
  15. } else {
  16. cout << "Nilai nol" << endl;
  17. }
  18.  
  19. // Struktur perulangan (for)
  20. cout << "Deret angka dari 1 sampai 5: " << endl;
  21. for (int i = 1; i <= 5; i++) {
  22. cout << i << " ";
  23. }
  24. cout << endl;
  25.  
  26. // Struktur perulangan (while)
  27. int j = 1;
  28. cout << "Deret angka dari 1 sampai 5 (dengan while): " << endl;
  29. while (j <= 5) {
  30. cout << j << " ";
  31. j++;
  32. }
  33. cout << endl;
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
Masukkan nilai: Nilai positif
Deret angka dari 1 sampai 5: 
1 2 3 4 5 
Deret angka dari 1 sampai 5 (dengan while): 
1 2 3 4 5