24.3k views
0 votes
Write a program in C++ that will ask the user to enter a five-digit integer. The program will detect if the number entered is a palindrome or not. Make sure the user enters a five-digit integer, not five single digit integers.

The program must have validation; that is, it must ensure the value entered is a five-digit integer (i.e. an integer between 10000 and 99999.

User Nall
by
5.5k points

1 Answer

6 votes

Answer:

//here is code in C++.

#include <bits/stdc++.h>

using namespace std;

int main()

{

// variables

int num,num_copy;

int new_num=0;

cout<<"enter a five digit number: ";

// read number from user

cin>>num;

// check input is a 5 digit number or not

while(num<10000||num>99999)

{

cout<<"Wrong input!!"<<endl;

cout<<"enter a five digit number only: ";

// if input is wrong it will again ask user to enter 5 digit number

cin>>num;

}

// make a copy of input number

num_copy=num;

//reverse the digits of input and make a new 5 digit number

while(num>0)

{

int r=num%10;

new_num=new_num*10+r;

num=num/10;

}

// check if both number are same or not

// is same then print palindrome else not palindrome

if(num_copy==new_num)

cout<<"number is palindrome"<<endl;

else

cout<<"number is not palindrome"<<endl;

return 0;

}

Step-by-step explanation:

Read a five digits number from user.If input is not a five digits number then it will ask again to enter a five digit number until user enter the correct input. Then it make a copy of the input number.It will make a new five digit number by reversing the digits of input number.Then it will match both the number, if they match then it will print "palindrome" else "not palindrome".

Output:

enter a five digit number: 123 Wrong input!! enter a five digit number only: 222221 Wrong input!! enter a five digit number only: 23432 number is palindrome

enter a five digit number: 12345 number is not palindrome

User Chris Conover
by
5.1k points