Skip to content
Related Articles

Related Articles

Improve Article

Check if the number is balanced

  • Last Updated : 07 May, 2021

Given a number N in the form of a string, the task is to check whether the given number is balanced or not.

Balanced Number: A number is said to be balanced if the sum of digits in the first half of it is equal to the sum of the digits in the second half. 
Note: All Palindromic numbers are balanced numbers. 

Examples:

Input: N = 19091 
Output: Balanced 
Explanation: 
middle element is 0 
Sum of left half = 1 + 9 = 10 
Sum of right half = 9 + 1 = 10 
Hence, the given number is a Balanced number.

Input: N = 133423 
Output: Not Balanced 
Explanation: 
Sum of left half = 1 + 3 + 3 (7) 
Sum of right half = 4 + 2 + 3 (9) 
Hence, the given number is not Balanced 



Approach:
Iterate over half the length of the number from the beginning. Calculate the sum of digits of the first half and the second half simultaneously by adding s[i] and s[number of digits – 1 – i] to leftSum and rightSum respectively. Finally, check if the leftSum and rightSum are equal or not.

Below is the implementation of the above approach.  

C++




// C++ program to check
// if a number is
// Balanced or not
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to check whether N is
// Balanced Number or not
void BalancedNumber(string s)
{
    int Leftsum = 0;
    int Rightsum = 0;
 
    // Calculating the Leftsum
    // and rightSum simultaneously
    for (int i = 0; i < s.size() / 2; i++) {
 
        // Typecasting each character
        // to integer and adding the
        // digit to respective sums
        Leftsum += int(s[i] - '0');
        Rightsum += int(s[s.size() - 1 - i]
                        - '0');
    }
 
    if (Leftsum == Rightsum)
        cout << "Balanced" << endl;
    else
        cout << "Not Balanced" << endl;
}
 
// Driver Code
int main()
{
    string s = "12321";
 
    // Function call
    BalancedNumber(s);
 
    return 0;
}

Java




// Java program to check if a number
// is Balanced or not
import java.io.*;
 
class GFG{
   
// Function to check whether N is
// Balanced Number or not
private static void BalancedNumber(String s)
{
    int Leftsum = 0;
    int Rightsum = 0;
     
    // Calculating the Leftsum
    // and rightSum simultaneously
    for(int i = 0; i < s.length() / 2; i++)
    {
         
        // Typecasting each character
        // to integer and adding the
        // digit to respective sums
        Leftsum += (int)(s.charAt(i) - '0');
        Rightsum += (int)(s.charAt(
            s.length() - 1 - i) - '0');
    }
     
    if (Leftsum == Rightsum)
        System.out.println("Balanced");
    else
        System.out.println("Not Balanced");
}
 
// Driver Code
public static void main (String[] args)
{
    String s = "12321";
     
    // Function call
    BalancedNumber(s);
}
}
 
// This code is contributed by jithin

Python3




# Python3 program to check
# if a number is
# Balanced or not
 
# Function to check whether N is
# Balanced Number or not
def BalancedNumber(s):
 
    Leftsum = 0
    Rightsum = 0
 
    # Calculating the Leftsum
    # and rightSum simultaneously
    for i in range(0, int(len(s) / 2)):
 
        # Typecasting each character
        # to integer and adding the
        # digit to respective sums
        Leftsum = Leftsum + int(s[i])
        Rightsum = (Rightsum +
                    int(s[len(s) - 1 - i]))
 
    if (Leftsum == Rightsum):
        print("Balanced", end = '\n')
    else:
        print("Not Balanced", end = '\n')
 
# Driver Code
s = "12321"
 
# Function call
BalancedNumber(s)
 
# This code is contributed by PratikBasu

C#




// C# program to check
// if a number is
// Balanced or not
using System;
class GFG{
     
// Function to check whether N is
// Balanced Number or not
static void BalancedNumber(string s)
{
  int Leftsum = 0;
  int Rightsum = 0;
 
  // Calculating the Leftsum
  // and rightSum simultaneously
  for (int i = 0; i < s.Length / 2; i++)
  {
    // Typecasting each character
    // to integer and adding the
    // digit to respective sums
    Leftsum += (int)(Char.GetNumericValue(s[i]) -
                     Char.GetNumericValue('0'));
    Rightsum += (int)(Char.GetNumericValue(s[s.Length -
                                             1 - i]) -
                      Char.GetNumericValue('0'));
  }
 
  if (Leftsum == Rightsum)
    Console.WriteLine("Balanced");
  else
    Console.WriteLine("Not Balanced");
 
// Driver code
static void Main()
{
  string s = "12321";
 
  // Function call
  BalancedNumber(s);
}
}
 
// This code is contributed by divyeshrabadiya07

Javascript




<script>
 
// JavaScript program to check if a number
// is Balanced or not
 
// Function to check whether N is
// Balanced Number or not
function BalancedNumber(s)
{
    let Leftsum = 0;
    let Rightsum = 0;
      
    // Calculating the Leftsum
    // and rightSum simultaneously
    for(let i = 0; i < s.length / 2; i++)
    {
          
        // Typecasting each character
        // to integer and adding the
        // digit to respective sums
        Leftsum += (s[i] - '0');
        Rightsum += (s[
            s.length - 1 - i] - '0');
    }
      
    if (Leftsum == Rightsum)
        document.write("Balanced");
    else
        document.write("Not Balanced");
}
     
// Driver Code
 
    let s = "12321";
      
    // Function call
    BalancedNumber(s);
           
</script>
Output: 
Balanced

 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :