Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Laboration 2, Assignment_3.cpp
- // Fabian Tjernström (fatj1700) 2019-01-19
- #include <iostream>
- using namespace std;
- int main() {
- bool loop = true;
- char yesNo;
- cout << "This is a program to check if a number is a palindrome." << endl;
- while(loop == true) {
- int num = 0, copyNum = 0, digitCount = 0, rem = 0, revNum = 0;
- cout << "Enter a five digit number: ";
- cin.clear();
- cin >> num;
- copyNum = num;
- while (copyNum > 0) {
- copyNum = copyNum / 10;
- digitCount++;
- }
- if (digitCount < 5) {
- cout << "Too FEW digits!" << endl;
- } else if (digitCount > 5) {
- cout << "Too MANY digits!" << endl;
- } else {
- copyNum = num;
- while (copyNum != 0) {
- rem = copyNum % 10;
- revNum = revNum * 10 + rem;
- copyNum /= 10;
- }
- if (revNum == num) {
- cout << endl;
- cout << "Your number is a palindrome!" << endl;
- cout << "The palindrome is: ";
- cout << revNum << endl;
- } else {
- cout << endl;
- cout << "Sorry, your number is NOT a palindrome." << endl;
- }
- }
- cout << endl;
- cout << "Repeat program? [y/n]: ";
- cin.clear();
- cin >> yesNo;
- if(yesNo == 'y') {
- loop = true;
- }
- else if(yesNo == 'n') {
- loop = false;
- }
- cout << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement