Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to check if two halves of a number are equal in C++

2 Answers

0 votes
#include <iostream>
#include <string>
#include <cmath>
 
bool checkHalvesEqual(long long n) {
    // Convert the number to a string to easily access digits.
    // Use abs() to handle negative numbers, as the sign doesn't affect digit comparison.
    std::string s = std::to_string(std::abs(n));
    int length = s.length();
    
    // Check if the number of digits is not equal (i.e., odd)
    if (length % 2 != 0) {
        std::cout << "The number of digits is NOT even. It cannot be split into two halves: ";
        return false;
    }
 
    // Calculate the length of one half. 
    int half_length = length / 2;
 
    // Extract the first half substring
    std::string first_half = s.substr(0, half_length);
 
    // Extract the second half substring
    // The second half starts from index (length - half_length)
    std::string second_half = s.substr(length - half_length);
 
    // Check if the two substrings are equal
    return first_half == second_half;
}
 
int main() {
    long long num1 = 123456; 
    long long num2 = 123321; 
    long long num3 = 123123; 
    long long num4 = 1234321;
    long long num5 = 12321; 
 
    std::cout << num1 << ": " << std::boolalpha << checkHalvesEqual(num1) << std::endl;
    std::cout << num2 << ": " << std::boolalpha << checkHalvesEqual(num2) << std::endl;
    std::cout << num3 << ": " << std::boolalpha << checkHalvesEqual(num3) << std::endl;
    std::cout << num4 << ": " << std::boolalpha << checkHalvesEqual(num4) << std::endl;
    std::cout << num5 << ": " << std::boolalpha << checkHalvesEqual(num5) << std::endl;
}
 
 
 
/*
run:
 
123456: false
123321: false
123123: true
1234321: The number of digits is NOT even. It cannot be split into two halves: false
12321: The number of digits is NOT even. It cannot be split into two halves: false
 
*/
 

 



answered Dec 20, 2025 by avibootz
edited Dec 20, 2025 by avibootz
0 votes
#include <iostream>
#include <string>
#include <cmath>

bool hasEqualHalves(long long n) {
    // 1. Get length by converting to string or using log10
    std::string s = std::to_string(std::abs(n));
    int length = s.length();

    // 2. Halves can only be equal if length is even
    if (length % 2 != 0) return false;

    // 3. Calculate divisor using 10^(length/2) + 1
    // Example: length 4 -> 10^(2) + 1 = 101
    // Example: length 6 -> 10^(3) + 1 = 1001
    long long halfLength = length / 2;
    long long divisor = std::pow(10, halfLength) + 1;

    // 4. Check divisibility
    return (n % divisor == 0);
}

int main() {
    long long testNumbers[] = {1212, 123123, 45454545, 123, 1213};

    for (long long num : testNumbers) {
        if (hasEqualHalves(num)) {
            std::cout << num << " has equal halves.\n";
        } else {
            std::cout << num << " does NOT have equal halves.\n";
        }
    }
}



/*
run:

1212 has equal halves.
123123 has equal halves.
45454545 has equal halves.
123 does NOT have equal halves.
1213 does NOT have equal halves.

*/

 



answered Dec 21, 2025 by avibootz
edited Dec 21, 2025 by avibootz
...