Answer to Question #49714 in C++ for nada

Question #49714
Write the following program:
A. Write a value‐returning function called length() that takes a positive integer and returns the number of digits in that integer.
B. Using the written function in part A, write a value‐returning function called isPalindrome() that checks if a positive integer is a palindrome or not. A number is palindrome if it reads the same left to right and right to left. For example 23432 is a palindrome, while 5675 is not palindrome. The function takes a positive integer and returns true if the integer is a palindrome and false otherwise.
Test your functions by writing a main function that prompts the user to input an integer and outputs its number of digits and "It Is a Palindrome" message if it is a palindrome, otherwise "It Is not a Palindrome".
1
Expert's answer
2014-12-05T01:25:33-0500
#include<stream.h>
#include<conio.h>

int length(int n)
{
int i=0;
while(n>0)
{
n/=10;
i++;
}
return i;
}

int isPalindrome(intn)
{
int i;
int len=length(n);
int*mas=(int* )malloc(len);
for(i=0;i<len;i++)
{
mas[i]=n;
n=n/10;
}
for(i=0;i<len/2;i++)
if(mas[i]!=mas[len-1-i])
return 0;
return 1;
}

int main()
{
int n;
cout<<"Input n: ";
cin>>n;
if(isPalindrome(n))
cout<<"It Is aPalindrome";
else
cout<<"It Is not aPalindrome";

getch();
return 1;

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS