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(),
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".

Dani AI

Generated

A compact, homework-friendly arithmetic solution that implements the two functions requested by . This example follows the spirit of earlier replies from , and (show effort), and offers an alternative to the string approach suggested by by comparing digits numerically (no string conversion).

The idea: length() counts digits by dividing by 10 repeatedly (zero returns 1). ispalindrome() uses the digit count and two divisors to compare the leftmost and rightmost digits pairwise, moving inward. This avoids reversing the whole number (so it stays safe for values that fit in unsigned long long) and runs in O(d) time where d is the number of digits.

#include <iostream>

unsigned int length(unsigned long long n) {
    if (n == 0) return 1;
    unsigned int cnt = 0;
    while (n > 0) { ++cnt; n /= 10; }
    return cnt;
}

bool ispalindrome(unsigned long long n) {
    unsigned int len = length(n);
    unsigned long long left_div = 1;
    for (unsigned int i = 1; i < len; ++i) left_div *= 10; // 10^(len-1)
    unsigned long long right_div = 1;
    for (unsigned int i = 0; i < len / 2; ++i) {
        unsigned int left_digit = (n / left_div) % 10;
        unsigned int right_digit = (n / right_div) % 10;
        if (left_digit != right_digit) return false;
        left_div /= 10;
        right_div *= 10;
    }
    return true;
}

int main() {
    long long t;
    std::cout << "Enter a positive integer: ";
    if (!(std::cin >> t)) return 0;
    if (t < 0) { std::cout << "Please enter a positive integer.\n"; return 0; }
    unsigned long long n = static_cast<unsigned long long>(t);
    std::cout << "Number of digits: " << length(n) << '\n';
    if (ispalindrome(n)) std::cout << "It Is a Palindrome\n";
    else std::cout << "It Is not a Palindrome\n";
    return 0;
}

Notes and caveats: this numeric approach avoids the simple std::string + std::reverse() trick (valid and shorter), and it prevents overflow issues that can occur if you fully reverse very large values. For arbitrarily large integers use a string-based check or a big-integer library. This implementation matches the assignment requirements while staying robust for typical inputs.

Recommended Answers

All 5 Replies

So what seams to be your problem? We do not give answers here but we do offer assistance.

What NathanOliver said, plus we aren't here to help you cheat on your homework assignments. Make an honest effort to solve the problem, post your work here, and we will make appropriate comments.

As NathanOliver and rubberman said, at least post your source code here. We need to see a bit of effort before we provide assistance.

Do your best and attempt each question. It's only when you encounter an issue that we can be of further assistance.
Moreover, there is plenty of resources around around daniweb. Search around and take bits and pieces and stick them together ;)

Member Avatar for Member #248612

Use two std:string and initalize them both with the word. Use std::reverse() on the second string. If both strings are same after that, the word is a palindrome

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.