Advertisement
fabbe680

2.3 v2

Jan 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. // Laboration 2, Assignment_3.cpp
  2. // Fabian Tjernström (fatj1700) 2019-01-19
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9.  
  10. bool loop = true;
  11. char yesNo;
  12.  
  13. cout << "This is a program to check if a number is a palindrome." << endl;
  14.  
  15. while(loop == true) {
  16.  
  17. int num = 0, copyNum = 0, digitCount = 0, rem = 0, revNum = 0;
  18.  
  19. cout << "Enter a five digit number: ";
  20. cin.clear();
  21. cin >> num;
  22.  
  23. copyNum = num;
  24.  
  25. while (copyNum > 0) {
  26. copyNum = copyNum / 10;
  27. digitCount++;
  28. }
  29.  
  30. if (digitCount < 5) {
  31. cout << "Too FEW digits!" << endl;
  32. } else if (digitCount > 5) {
  33. cout << "Too MANY digits!" << endl;
  34. } else {
  35. copyNum = num;
  36. while (copyNum != 0) {
  37. rem = copyNum % 10;
  38. revNum = revNum * 10 + rem;
  39. copyNum /= 10;
  40. }
  41. if (revNum == num) {
  42. cout << endl;
  43. cout << "Your number is a palindrome!" << endl;
  44. cout << "The palindrome is: ";
  45. cout << revNum << endl;
  46. } else {
  47. cout << endl;
  48. cout << "Sorry, your number is NOT a palindrome." << endl;
  49. }
  50. }
  51.  
  52. cout << endl;
  53. cout << "Repeat program? [y/n]: ";
  54. cin.clear();
  55. cin >> yesNo;
  56. if(yesNo == 'y') {
  57. loop = true;
  58. }
  59. else if(yesNo == 'n') {
  60. loop = false;
  61. }
  62.  
  63. cout << endl;
  64. }
  65.  
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement