Guest User

Purva

a guest
Nov 9th, 2019
104
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. bool hasRepeatedDigit(int);
  6.  
  7. int main() {
  8. int num;
  9. cin >> num;
  10. if (hasRepeatedDigit(num)) {
  11. cout << "Contains a digit twice";
  12. } else {
  13. cout << "Digits don't repeat";
  14. }
  15. return 0;
  16. }
  17.  
  18. bool hasRepeatedDigit(int num) {
  19. int countOne = 0;
  20. int countTwo = 0;
  21. int countThree = 0;
  22. int countFour = 0;
  23. int countFive = 0;
  24. int countSix = 0;
  25. int countSeven = 0;
  26. int countEight = 0;
  27. int countNine = 0;
  28. int countZero = 0;
  29.  
  30. while (num > 0) {
  31. int lastDigit = num % 10;
  32.  
  33. switch (lastDigit) {
  34. case 0:
  35. countZero++;
  36. if (countZero == 2) {
  37. return true;
  38. }
  39. break;
  40. case 1:
  41. countOne++;
  42. if (countOne == 2) {
  43. return true;
  44. }
  45. break;
  46. case 2:
  47. countTwo++;
  48. if (countTwo == 2) {
  49. return true;
  50. }
  51. break;
  52. case 3:
  53. countThree++;
  54. if (countThree == 2) {
  55. return true;
  56. }
  57. break;
  58. case 4:
  59. countFour++;
  60. if (countFour == 2) {
  61. return true;
  62. }
  63. break;
  64. case 5:
  65. countFive++;
  66. if (countFive == 2) {
  67. return true;
  68. }
  69. break;
  70. case 6:
  71. countSix++;
  72. if (countSix == 2) {
  73. return true;
  74. }
  75. break;
  76. case 7:
  77. countSeven++;
  78. if (countSeven == 2) {
  79. return true;
  80. }
  81. break;
  82. case 8:
  83. countEight++;
  84. if (countEight == 2) {
  85. return true;
  86. }
  87. break;
  88. case 9:
  89. countNine++;
  90. if (countNine == 2) {
  91. return true;
  92. }
  93. break;
  94. }
  95.  
  96. if (lastDigit == 1) {
  97. countOne++;
  98. if (countOne == 2) {
  99. return true;
  100. }
  101. } else if (lastDigit == 2) {
  102. countTwo++;
  103. if (countTwo == 2) {
  104. return true;
  105. }
  106. } else if (lastDigit == 3) {
  107. countThree++;
  108. if (countThree == 2) {
  109. return true;
  110. }
  111. } else if (lastDigit == 4) {
  112. countFour++;
  113. if (countFour == 2) {
  114. return true;
  115. }
  116. } else if (lastDigit == 5) {
  117. countFive++;
  118. if (countFive == 2) {
  119. return true;
  120. }
  121. } else if (lastDigit == 6) {
  122. countSix++;
  123. if (countSix == 2) {
  124. return true;
  125. }
  126. } else if (lastDigit == 7) {
  127. countSeven++;
  128. if (countSeven == 2) {
  129. return true;
  130. }
  131. } else if (lastDigit == 8) {
  132. countEight++;
  133. if (countEight == 2) {
  134. return true;
  135. }
  136. } else if (lastDigit == 9) {
  137. countEight++;
  138. if (countEight == 2) {
  139. return true;
  140. }
  141. }
  142. num /= 10;
  143. }
  144.  
  145. return false;
  146. }
RAW Paste Data