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; }
Numbers and figures are an essential part of our world, necessary for almost everything we do every day. As important…
APPROVED BY CLIENTS
Finding a professional expert in "partial differential equations" in the advanced level is difficult.
You can find this expert in "Assignmentexpert.com" with confidence.
Exceptional experts! I appreciate your help. God bless you!
Comments
Leave a comment