fork download
  1. #include <iostream>
  2. #include <stdexcept>
  3. using namespace std;
  4.  
  5. void tekaTekiTeko(unsigned int batas) {
  6. // negative number will wrap around to a very large positive due to unsigned nature
  7.  
  8. if (batas < 20) throw invalid_argument("Nilai batas harus lebih besar atau sama dengan 20");
  9.  
  10. for (unsigned int i = 1; i <= batas; ++i) {
  11. if (i % 2 == 0 && i % 3 == 0 && i % 5 == 0) {
  12. cout << "TekaTekiTeko" << endl;
  13. } else if (i % 2 == 0 && i % 3 == 0) {
  14. cout << "TekaTeki" << endl;
  15. } else if (i % 2 == 0 && i % 5 == 0) {
  16. cout << "TekaTeko" << endl;
  17. } else if (i % 3 == 0 && i % 5 == 0) {
  18. cout << "TekiTeko" << endl;
  19. } else if (i % 2 == 0) {
  20. cout << "Teka" << endl;
  21. } else if (i % 3 == 0) {
  22. cout << "Teki" << endl;
  23. } else if (i % 5 == 0) {
  24. cout << "Teko" << endl;
  25. } else {
  26. cout << i << endl;
  27. }
  28. }
  29. }
  30.  
  31. int main() {
  32. unsigned int input;
  33. cout << "Masukkan batas: ";
  34. cin >> input;
  35.  
  36. if (cin.fail()) {
  37. cerr << "Error: Input bukan unsigned integer." << endl;
  38. return 1;
  39. }
  40.  
  41. try {
  42. tekaTekiTeko(input);
  43. } catch (const invalid_argument& e) {
  44. cerr << "Error: " << e.what() << endl;
  45. return 1;
  46. }
  47.  
  48. return 0;
  49. }
  50.  
Success #stdin #stdout 0s 5316KB
stdin
30
stdout
Masukkan batas: 1
Teka
Teki
Teka
Teko
TekaTeki
7
Teka
Teki
TekaTeko
11
TekaTeki
13
Teka
TekiTeko
Teka
17
TekaTeki
19
TekaTeko
Teki
Teka
23
TekaTeki
Teko
Teka
Teki
Teka
29
TekaTekiTeko