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.

40,023 questions

51,974 answers

573 users

How to check if the sum of two halves of a number is equal in C#

2 Answers

0 votes
using System;

class Program
{
    static int SumDigits(long n) {
        int sum = 0;

        while (n != 0) {
            sum += (int)(n % 10);
            n /= 10;
        }

        return sum;
    }

    static bool CheckHalvesSumEqual(long n) {
        string s = Math.Abs(n).ToString();
        int length = s.Length;

        if (length % 2 != 0) {
            Console.Write("The number of digits is NOT even. It cannot be split into two halves: ");
            return false;
        }

        int halfLength = length / 2;

        string firstHalf = s.Substring(0, halfLength);
        string secondHalf = s.Substring(length - halfLength);

        long num1 = long.Parse(firstHalf);
        long num2 = long.Parse(secondHalf);

        return SumDigits(num1) == SumDigits(num2);
    }

    static void Main()
    {
        long num1 = 123456;
        long num2 = 123321;
        long num3 = 123123;
        long num4 = 123411;
        long num5 = 1234321;
        long num6 = 12321;

        Console.WriteLine($"{num1}: {CheckHalvesSumEqual(num1).ToString().ToLower()}");
        Console.WriteLine($"{num2}: {CheckHalvesSumEqual(num2).ToString().ToLower()}");
        Console.WriteLine($"{num3}: {CheckHalvesSumEqual(num3).ToString().ToLower()}");
        Console.WriteLine($"{num4}: {CheckHalvesSumEqual(num4).ToString().ToLower()}");
        Console.WriteLine($"{num5}: {CheckHalvesSumEqual(num5).ToString().ToLower()}");
        Console.WriteLine($"{num6}: {CheckHalvesSumEqual(num6).ToString().ToLower()}");
    }
}



/*
run:

123456: false
123321: true
123123: true
123411: true
The number of digits is NOT even. It cannot be split into two halves: 1234321: false
The number of digits is NOT even. It cannot be split into two halves: 12321: false

*/

 



answered Dec 22, 2025 by avibootz
0 votes
using System;
using System.Linq;

class Program
{
    static bool HalvesSumEqual(long n) {
        var s = Math.Abs(n).ToString();
        if (s.Length % 2 != 0)
            return false;
    
        int half = s.Length / 2;
    
        int leftSum  = s[..half].Sum(c => c - '0');
        int rightSum = s[half..].Sum(c => c - '0');
    
        return leftSum == rightSum;
    }


    static void Main()
    {
        var nums = new long[] { 123456, 123321, 123123, 123411, 1234321, 12321 };

        foreach (var n in nums) {
            Console.WriteLine($"{n}: {HalvesSumEqual(n).ToString().ToLower()}");
        }
    }
}



/*
run:

123456: false
123321: true
123123: true
123411: true
1234321: false
12321: false

*/

 



answered Dec 23, 2025 by avibootz
...