Check if the number is balanced

Here given code implementation process.

/*
  C Program
  Check if the number is balanced
*/

#include <stdio.h>

// When number is negative, then returning it positive form
int valid_positive(int num)
{
	if (num < 0)
	{
		//When number is negative 
		return -num;
	}
	else
	{
		return num;
	}
}
//Check whether given number is balanced or not
void balanced_number(int num)
{
	int x = valid_positive(num);
	// Define resultant variable
	int left = 0;
	int right = 0;
	//Count number of digits
	int size = 0;
	while (x != 0)
	{
		size++;
		x /= 10;
	}
	x = valid_positive(num);
	int extra = 0;
	if (size % 2 != 0)
	{
		//Handles the request of odd length number
		extra = -1;
	}
	//loop controlling variable i
	int i = 1;
	while (x != 0)
	{
		if (i <= (size / 2))
		{
			// Calculate sum of right half digits
			right += x % 10;
		}
		else if ((i + extra) > (size / 2))
		{
			// Calculate sum of left half digits
			left += x % 10;
		}
		//remove last digit
		x /= 10;
		i++;
	}
	if (left == right)
	{
		printf(" [%d] Balanced \n", num);
	}
	else
	{
		printf(" [%d] Not Balanced \n", num);
	}
}
int main()
{
	//Test Case
	balanced_number(1439125);
	balanced_number(131);
	balanced_number(1235);
	balanced_number(24253);
	balanced_number(1230);
	balanced_number(123501);
	return 0;
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
/*
    Java Program
    Check if the number is balanced
*/
class BalanceNumber
{
	// When number is negative, then returning it positive form
	public int valid_positive(int num)
	{
		if (num < 0)
		{
			//When number is negative 
			return -num;
		}
		else
		{
			return num;
		}
	}
	//Check whether given number is balanced or not
	public void balanced_number(int num)
	{
		int x = valid_positive(num);
		// Define resultant variable
		int left = 0;
		int right = 0;
		//Count number of digits
		int size = 0;
		while (x != 0)
		{
			size++;
			x /= 10;
		}
		x = valid_positive(num);
		int extra = 0;
		if (size % 2 != 0)
		{
			//Handles the request of odd length number
			extra = -1;
		}
		//loop controlling variable i
		int i = 1;
		while (x != 0)
		{
			if (i <= (size / 2))
			{
				// Calculate sum of right half digits
				right += x % 10;
			}
			else if ((i + extra) > (size / 2))
			{
				// Calculate sum of left half digits
				left += x % 10;
			}
			//remove last digit
			x /= 10;
			i++;
		}
		if (left == right)
		{
			System.out.print(" [" + num + "] Balanced \n");
		}
		else
		{
			System.out.print(" [" + num + "] Not Balanced \n");
		}
	}
	public static void main(String[] args)
	{
		BalanceNumber obj = new BalanceNumber();
		//Test Case
		obj.balanced_number(1439125);
		obj.balanced_number(131);
		obj.balanced_number(1235);
		obj.balanced_number(24253);
		obj.balanced_number(1230);
		obj.balanced_number(123501);
	}
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
//Include header file
#include <iostream>
using namespace std;

/*
    C++ Program
    Check if the number is balanced
*/

class BalanceNumber
{
	public:
		// When number is negative, then returning it positive form
		int valid_positive(int num)
		{
			if (num < 0)
			{
				//When number is negative 
				return -num;
			}
			else
			{
				return num;
			}
		}
	//Check whether given number is balanced or not
	void balanced_number(int num)
	{
		int x = this->valid_positive(num);
		// Define resultant variable
		int left = 0;
		int right = 0;
		//Count number of digits
		int size = 0;
		while (x != 0)
		{
			size++;
			x /= 10;
		}
		x = this->valid_positive(num);
		int extra = 0;
		if (size % 2 != 0)
		{
			//Handles the request of odd length number
			extra = -1;
		}
		//loop controlling variable i
		int i = 1;
		while (x != 0)
		{
			if (i <= (size / 2))
			{
				// Calculate sum of right half digits
				right += x % 10;
			}
			else if ((i + extra) > (size / 2))
			{
				// Calculate sum of left half digits
				left += x % 10;
			}
			//remove last digit
			x /= 10;
			i++;
		}
		if (left == right)
		{
			cout << " [" << num << "] Balanced \n";
		}
		else
		{
			cout << " [" << num << "] Not Balanced \n";
		}
	}
};
int main()
{
	BalanceNumber obj = BalanceNumber();
	//Test Case
	obj.balanced_number(1439125);
	obj.balanced_number(131);
	obj.balanced_number(1235);
	obj.balanced_number(24253);
	obj.balanced_number(1230);
	obj.balanced_number(123501);
	return 0;
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
//Include namespace system
using System;
/*
    C# Program
    Check if the number is balanced
*/
class BalanceNumber
{
	// When number is negative, then returning it positive form
	public int valid_positive(int num)
	{
		if (num < 0)
		{
			//When number is negative 
			return -num;
		}
		else
		{
			return num;
		}
	}
	//Check whether given number is balanced or not
	public void balanced_number(int num)
	{
		int x = valid_positive(num);
		// Define resultant variable
		int left = 0;
		int right = 0;
		//Count number of digits
		int size = 0;
		while (x != 0)
		{
			size++;
			x /= 10;
		}
		x = valid_positive(num);
		int extra = 0;
		if (size % 2 != 0)
		{
			//Handles the request of odd length number
			extra = -1;
		}
		//loop controlling variable i
		int i = 1;
		while (x != 0)
		{
			if (i <= (size / 2))
			{
				// Calculate sum of right half digits
				right += x % 10;
			}
			else if ((i + extra) > (size / 2))
			{
				// Calculate sum of left half digits
				left += x % 10;
			}
			//remove last digit
			x /= 10;
			i++;
		}
		if (left == right)
		{
			Console.Write(" [" + num + "] Balanced \n");
		}
		else
		{
			Console.Write(" [" + num + "] Not Balanced \n");
		}
	}
	public static void Main(String[] args)
	{
		BalanceNumber obj = new BalanceNumber();
		//Test Case
		obj.balanced_number(1439125);
		obj.balanced_number(131);
		obj.balanced_number(1235);
		obj.balanced_number(24253);
		obj.balanced_number(1230);
		obj.balanced_number(123501);
	}
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
<?php
/*
    Php Program
    Check if the number is balanced
*/
class BalanceNumber
{
	// When number is negative, then returning it positive form
	public	function valid_positive($num)
	{
		if ($num < 0)
		{
			//When number is negative 
			return -$num;
		}
		else
		{
			return $num;
		}
	}
	//Check whether given number is balanced or not
	public	function balanced_number($num)
	{
		$x = $this->valid_positive($num);
		// Define resultant variable
		$left = 0;
		$right = 0;
		//Count number of digits
		$size = 0;
		while ($x != 0)
		{
			$size++;
			$x = intval($x / 10);
		}
		$x = $this->valid_positive($num);
		$extra = 0;
		if ($size % 2 != 0)
		{
			//Handles the request of odd length number
			$extra = -1;
		}
		//loop controlling variable i
		$i = 1;
		while ($x != 0)
		{
			if ($i <= (intval($size / 2)))
			{
				// Calculate sum of right half digits
				$right += $x % 10;
			}
			else if (($i + $extra) > (intval($size / 2)))
			{
				// Calculate sum of left half digits
				$left += $x % 10;
			}
			$x = intval($x /
				//remove last digit
				10);
			$i++;
		}
		if ($left == $right)
		{
			echo " [". $num ."] Balanced \n";
		}
		else
		{
			echo " [". $num ."] Not Balanced \n";
		}
	}
}

function main()
{
	$obj = new BalanceNumber();
	//Test Case
	$obj->balanced_number(1439125);
	$obj->balanced_number(131);
	$obj->balanced_number(1235);
	$obj->balanced_number(24253);
	$obj->balanced_number(1230);
	$obj->balanced_number(123501);
}
main();

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
/*
    Node Js Program
    Check if the number is balanced
*/
class BalanceNumber
{
	// When number is negative, then returning it positive form
	valid_positive(num)
	{
		if (num < 0)
		{
			//When number is negative 
			return -num;
		}
		else
		{
			return num;
		}
	}
	//Check whether given number is balanced or not
	balanced_number(num)
	{
		var x = this.valid_positive(num);
		// Define resultant variable
		var left = 0;
		var right = 0;
		//Count number of digits
		var size = 0;
		while (x != 0)
		{
			size++;
			x = parseInt(x / 10);
		}
		x = this.valid_positive(num);
		var extra = 0;
		if (size % 2 != 0)
		{
			//Handles the request of odd length number
			extra = -1;
		}
		//loop controlling variable i
		var i = 1;
		while (x != 0)
		{
			if (i <= (parseInt(size / 2)))
			{
				// Calculate sum of right half digits
				right += x % 10;
			}
			else if ((i + extra) > (parseInt(size / 2)))
			{
				// Calculate sum of left half digits
				left += x % 10;
			}
			x = parseInt(x /
				//remove last digit
				10);
			i++;
		}
		if (left == right)
		{
			process.stdout.write(" [" + num + "] Balanced \n");
		}
		else
		{
			process.stdout.write(" [" + num + "] Not Balanced \n");
		}
	}
}

function main()
{
	var obj = new BalanceNumber();
	//Test Case
	obj.balanced_number(1439125);
	obj.balanced_number(131);
	obj.balanced_number(1235);
	obj.balanced_number(24253);
	obj.balanced_number(1230);
	obj.balanced_number(123501);
}
main();

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
#     Python 3 Program
#     Check if the number is balanced

class BalanceNumber :
	#  When number is negative, then returning it positive form
	def valid_positive(self, num) :
		if (num < 0) :
			# When number is negative 
			return -num
		else :
			return num
		
	
	# Check whether given number is balanced or not
	def balanced_number(self, num) :
		x = self.valid_positive(num)
		#  Define resultant variable
		left = 0
		right = 0
		# Count number of digits
		size = 0
		while (x != 0) :
			size += 1
			x = int(x / 10)
		
		x = self.valid_positive(num)
		extra = 0
		if (size % 2 != 0) :
			# Handles the request of odd length number
			extra = -1
		
		# loop controlling variable i
		i = 1
		while (x != 0) :
			if (i <= (int(size / 2))) :
				#  Calculate sum of right half digits
				right += x % 10
			
			elif((i + extra) > (int(size / 2))) :
				#  Calculate sum of left half digits
				left += x % 10
			
			x = int(x /
				# remove last digit
				10)
			i += 1
		
		if (left == right) :
			print(" [", num ,"] Balanced \n", end = "")
		else :
			print(" [", num ,"] Not Balanced \n", end = "")
		
	

def main() :
	obj = BalanceNumber()
	# Test Case
	obj.balanced_number(1439125)
	obj.balanced_number(131)
	obj.balanced_number(1235)
	obj.balanced_number(24253)
	obj.balanced_number(1230)
	obj.balanced_number(123501)

if __name__ == "__main__": main()

Output

 [ 1439125 ] Balanced
 [ 131 ] Balanced
 [ 1235 ] Not Balanced
 [ 24253 ] Not Balanced
 [ 1230 ] Balanced
 [ 123501 ] Balanced
#  Ruby Program
#  Check if the number is balanced

class BalanceNumber 
	#  When number is negative, then returning it positive form
	def valid_positive(num) 
		if (num < 0) 
			# When number is negative 
			return -num
		else 
			return num
		end

	end

	# Check whether given number is balanced or not
	def balanced_number(num) 
		x = self.valid_positive(num)
		#  Define resultant variable
		left = 0
		right = 0
		# Count number of digits
		size = 0
		while (x != 0) 
			size += 1
			x /= 10
		end

		x = self.valid_positive(num)
		extra = 0
		if (size % 2 != 0) 
			# Handles the request of odd length number
			extra = -1
		end

		# loop controlling variable i
		i = 1
		while (x != 0) 
			if (i <= (size / 2)) 
				#  Calculate sum of right half digits
				right += x % 10
			elsif((i + extra) > (size / 2)) 
				#  Calculate sum of left half digits
				left += x % 10
			end

			# remove last digit
			x /= 10
			i += 1
		end

		if (left == right) 
			print(" [", num ,"] Balanced \n")
		else 
			print(" [", num ,"] Not Balanced \n")
		end

	end

end

def main() 
	obj = BalanceNumber.new()
	# Test Case
	obj.balanced_number(1439125)
	obj.balanced_number(131)
	obj.balanced_number(1235)
	obj.balanced_number(24253)
	obj.balanced_number(1230)
	obj.balanced_number(123501)
end

main()

Output

 [1439125] Balanced 
 [131] Balanced 
 [1235] Not Balanced 
 [24253] Not Balanced 
 [1230] Balanced 
 [123501] Balanced 
/*
    Scala Program
    Check if the number is balanced
*/
class BalanceNumber
{
	// When number is negative, then returning it positive form
	def valid_positive(num: Int): Int = {
		if (num < 0)
		{
			//When number is negative 
			return -num;
		}
		else
		{
			return num;
		}
	}
	//Check whether given number is balanced or not
	def balanced_number(num: Int): Unit = {
		var x: Int = valid_positive(num);
		// Define resultant variable
		var left: Int = 0;
		var right: Int = 0;
		//Count number of digits
		var size: Int = 0;
		while (x != 0)
		{
			size += 1;
			x = (x / 10).toInt;
		}
		x = valid_positive(num);
		var extra: Int = 0;
		if (size % 2 != 0)
		{
			//Handles the request of odd length number
			extra = -1;
		}
		//loop controlling variable i
		var i: Int = 1;
		while (x != 0)
		{
			if (i <= ((size / 2).toInt))
			{
				// Calculate sum of right half digits
				right += x % 10;
			}
			else if ((i + extra) > ((size / 2).toInt))
			{
				// Calculate sum of left half digits
				left += x % 10;
			}
			x = (x /
				//remove last digit
				10).toInt;
			i += 1;
		}
		if (left == right)
		{
			print(" [" + num + "] Balanced \n");
		}
		else
		{
			print(" [" + num + "] Not Balanced \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: BalanceNumber = new BalanceNumber();
		//Test Case
		obj.balanced_number(1439125);
		obj.balanced_number(131);
		obj.balanced_number(1235);
		obj.balanced_number(24253);
		obj.balanced_number(1230);
		obj.balanced_number(123501);
	}
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
/*
    Swift 4 Program
    Check if the number is balanced
*/
class BalanceNumber
{
	// When number is negative, then returning it positive form
	func valid_positive(_ num: Int) -> Int
	{
		if (num < 0)
		{
			//When number is negative 
			return -num;
		}
		else
		{
			return num;
		}
	}
	//Check whether given number is balanced or not
	func balanced_number(_ num: Int)
	{
		var x: Int = self.valid_positive(num);
		// Define resultant variable
		var left: Int = 0;
		var right: Int = 0;
		//Count number of digits
		var size: Int = 0;
		while (x != 0)
		{
			size += 1;
			x /= 10;
		}
		x = self.valid_positive(num);
		var extra: Int = 0;
		if (size % 2 != 0)
		{
			//Handles the request of odd length number
			extra = -1;
		}
		//loop controlling variable i
		var i: Int = 1;
		while (x != 0)
		{
			if (i <= (size / 2))
			{
				// Calculate sum of right half digits
				right += x % 10;
			}
			else if ((i + extra) > (size / 2))
			{
				// Calculate sum of left half digits
				left += x % 10;
			}
			//remove last digit
			x /= 10;
			i += 1;
		}
		if (left == right)
		{
			print(" [", num ,"] Balanced \n", terminator: "");
		}
		else
		{
			print(" [", num ,"] Not Balanced \n", terminator: "");
		}
	}
}
func main()
{
	let obj: BalanceNumber = BalanceNumber();
	//Test Case
	obj.balanced_number(1439125);
	obj.balanced_number(131);
	obj.balanced_number(1235);
	obj.balanced_number(24253);
	obj.balanced_number(1230);
	obj.balanced_number(123501);
}
main();

Output

 [ 1439125 ] Balanced
 [ 131 ] Balanced
 [ 1235 ] Not Balanced
 [ 24253 ] Not Balanced
 [ 1230 ] Balanced
 [ 123501 ] Balanced
fn main()
{
	//Test Case
	balanced_number(1439125);
	balanced_number(131);
	balanced_number(1235);
	balanced_number(24253);
	balanced_number(1230);
	balanced_number(123501);
}
fn balanced_number(num: i32)
{
	let mut x: i32 = valid_positive(num);
	// Define resultant variable
	let mut left: i32 = 0;
	let mut right: i32 = 0;
	//Count number of digits
	let mut size: i32 = 0;
	while x != 0
	{
		size += 1;
		x /= 10;
	}
	x = valid_positive(num);
	let mut extra: i32 = 0;
	if size % 2 != 0
	{
		//Handles the request of odd length number
		extra = -1;
	}
	//loop controlling variable i
	let mut i: i32 = 1;
	while x != 0
	{
		if i <= (size / 2)
		{
			// Calculate sum of right half digits
			right += x % 10;
		}
		else if (i + extra) > (size / 2)
		{
			// Calculate sum of left half digits
			left += x % 10;
		}
		//remove last digit
		x /= 10;
		i += 1;
	}
	if left == right
	{
		print!(" [{}] Balanced \n", num);
	}
	else
	{
		print!(" [{}] Not Balanced \n", num);
	}
}
fn valid_positive(num: i32) -> i32
{
	if num < 0
	{
		//When number is negative 
		return -num;
	}
	else
	{
		return num;
	}
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced

When balance number is too large then we can pass number as string. let see an program.

/*
  C Program
  Check if the number is balanced
*/
#include <stdio.h>

int is_valid_number(char str_num[], int size)
{
	if (str_num[0] == '0')
	{
		// is start with zero
		// not a valid number
		return 0;
	}
	for (int i = 0; i < size; ++i)
	{
		if (!(str_num[i] >= '0' && str_num[i] <= '9'))
		{
			return 0;
		}
	}
	return 1;
}
//
void balanced_number(char str_num[], int size)
{
	if (size <= 0)
	{
		return;
	}
	//Assuming that the given number is valid positive number
	//Otherwise check
	if (is_valid_number(str_num, size) == 0)
	{
		//When not valid
		return;
	}
	int left = 0;
	int right = 0;
	for (int i = 0; i < size / 2; ++i)
	{
		// Calculate sum of left half digits
		left += (int)(str_num[i] - '0');
		// Calculate sum of right half digits
		right += (int)(str_num[(size - 1) - i] - '0');
	}
	if (left == right)
	{
		printf(" [%s] Balanced\n", str_num);
	}
	else
	{
		printf(" [%s] Not Balanced\n", str_num);
	}
}
int main()
{
	//Define number collection 
	char str_num1[] = "1439125";
	char str_num2[] = "131";
	char str_num3[] = "1235";
	char str_num4[] = "24253";
	char str_num5[] = "1230";
	char str_num6[] = "123501";
	char str_num7[] = "1234567895987654321";
	int size = 0;
	//Test Cases
	size = sizeof(str_num1) / sizeof(str_num1[0]) - 1;
	balanced_number(str_num1, size);
	size = sizeof(str_num2) / sizeof(str_num2[0]) - 1;
	balanced_number(str_num2, size);
	size = sizeof(str_num3) / sizeof(str_num3[0]) - 1;
	balanced_number(str_num3, size);
	size = sizeof(str_num4) / sizeof(str_num4[0]) - 1;
	balanced_number(str_num4, size);
	size = sizeof(str_num5) / sizeof(str_num5[0]) - 1;
	balanced_number(str_num5, size);
	size = sizeof(str_num6) / sizeof(str_num6[0]) - 1;
	balanced_number(str_num6, size);
	size = sizeof(str_num7) / sizeof(str_num7[0]) - 1;
	balanced_number(str_num7, size);
	return 0;
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
 [1234567895987654321] Balanced
/*
    Java Program
    Check if the number is balanced
    // When number is an string
*/
class BalanceNumber
{
	public boolean is_valid_number(String num, int size)
	{
		if (num.charAt(0) == '0')
		{
			// is start with zero
			// not a valid number
			return false;
		}
		for (int i = 0; i < size; ++i)
		{
			if (!(num.charAt(i) >= '0' && num.charAt(i) <= '9'))
			{
				return false;
			}
		}
		return true;
	}
	//Check whether given number is balanced or not
	public void balanced_number(String num)
	{
		int size = num.length();
		if (size == 0 || is_valid_number(num, size) == false)
		{
			return;
		}
		int left = 0;
		int right = 0;
		for (int i = 0; i < size / 2; ++i)
		{
			// Calculate sum of left half digits
			left += (int)(num.charAt(i) - '0');
			// Calculate sum of right half digits
			right += (int)(num.charAt((size - 1) - i) - '0');
		}
		if (left == right)
		{
			System.out.print(" [" + num + "] Balanced \n");
		}
		else
		{
			System.out.print(" [" + num + "] Not Balanced \n");
		}
	}
	public static void main(String[] args)
	{
		BalanceNumber obj = new BalanceNumber();
		//Test Case
		obj.balanced_number("1439125");
		obj.balanced_number("131");
		obj.balanced_number("1235");
		obj.balanced_number("24253");
		obj.balanced_number("1230");
		obj.balanced_number("123501");
		obj.balanced_number("1234567895987654321");
	}
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
 [1234567895987654321] Balanced
//Include header file
#include <iostream>
#include<string.h>

using namespace std;
/*
    C++ Program
    Check if the number is balanced
    // When number is an string
*/
class BalanceNumber
{
	public: bool is_valid_number(string num, int size)
	{
		if (num[0] == '0')
		{
			// is start with zero
			// not a valid number
			return false;
		}
		for (int i = 0; i < size; ++i)
		{
			if (!(num[i] >= '0' && num[i] <= '9'))
			{
				return false;
			}
		}
		return true;
	}
	//Check whether given number is balanced or not
	void balanced_number(string num)
	{
		int size = num.size();
		if (size == 0 || this->is_valid_number(num, size) == false)
		{
			return;
		}
		int left = 0;
		int right = 0;
		for (int i = 0; i < size / 2; ++i)
		{
			// Calculate sum of left half digits
			left += (int)(num[i] - '0');
			// Calculate sum of right half digits
			right += (int)((num[size - 1 - i]) - '0');
		}
		if (left == right)
		{
			cout << " [" << num << "] Balanced \n";
		}
		else
		{
			cout << " [" << num << "] Not Balanced \n";
		}
	}
};
int main()
{
	BalanceNumber obj = BalanceNumber();
	//Test Case
	obj.balanced_number("1439125");
	obj.balanced_number("131");
	obj.balanced_number("1235");
	obj.balanced_number("24253");
	obj.balanced_number("1230");
	obj.balanced_number("123501");
	obj.balanced_number("1234567895987654321");
	return 0;
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
 [1234567895987654321] Balanced
//Include namespace system
using System;
/*
    C# Program
    Check if the number is balanced
    // When number is an string
*/
class BalanceNumber
{
	public Boolean is_valid_number(String num, int size)
	{
		if (num[0] == '0')
		{
			// is start with zero
			// not a valid number
			return false;
		}
		for (int i = 0; i < size; ++i)
		{
			if (!(num[i] >= '0' && num[i] <= '9'))
			{
				return false;
			}
		}
		return true;
	}
	//Check whether given number is balanced or not
	public void balanced_number(String num)
	{
		int size = num.Length;
		if (size == 0 || is_valid_number(num, size) == false)
		{
			return;
		}
		int left = 0;
		int right = 0;
		for (int i = 0; i < size / 2; ++i)
		{
			// Calculate sum of left half digits
			left += (int)(num[i] - '0');
			// Calculate sum of right half digits
			right += (int)((num[size - 1 - i]) - '0');
		}
		if (left == right)
		{
			Console.Write(" [" + num + "] Balanced \n");
		}
		else
		{
			Console.Write(" [" + num + "] Not Balanced \n");
		}
	}
	public static void Main(String[] args)
	{
		BalanceNumber obj = new BalanceNumber();
		//Test Case
		obj.balanced_number("1439125");
		obj.balanced_number("131");
		obj.balanced_number("1235");
		obj.balanced_number("24253");
		obj.balanced_number("1230");
		obj.balanced_number("123501");
		obj.balanced_number("1234567895987654321");
	}
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
 [1234567895987654321] Balanced
<?php
/*
    Php Program
    Check if the number is balanced
    // When number is an string
*/
class BalanceNumber
{
	public	function is_valid_number($num, $size)
	{
		if ($num[0] == '0')
		{
			// is start with zero
			// not a valid number
			return false;
		}
		for ($i = 0; $i < $size; ++$i)
		{
			if (!(ord($num[$i]) >= ord('0') && ord($num[$i]) <= ord('9')))
			{
				return false;
			}
		}
		return true;
	}
	//Check whether given number is balanced or not
	public	function balanced_number($num)
	{
		$size = strlen($num);
		if ($size == 0 || $this->is_valid_number($num, $size) == false)
		{
			return;
		}
		$left = 0;
		$right = 0;
		for ($i = 0; $i < intval($size / 2); ++$i)
		{
			// Calculate sum of left half digits
			$left += (int)(ord($num[$i]) - ord('0'));
			// Calculate sum of right half digits
			$right += (int)((ord($num[$size - 1 - $i])) - ord('0'));
		}
		if ($left == $right)
		{
			echo " [". $num ."] Balanced \n";
		}
		else
		{
			echo " [". $num ."] Not Balanced \n";
		}
	}
}

function main()
{
	$obj = new BalanceNumber();
	//Test Case
	$obj->balanced_number("1439125");
	$obj->balanced_number("131");
	$obj->balanced_number("1235");
	$obj->balanced_number("24253");
	$obj->balanced_number("1230");
	$obj->balanced_number("123501");
	$obj->balanced_number("1234567895987654321");
}
main();

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
 [1234567895987654321] Balanced
/*
    Node Js Program
    Check if the number is balanced
    // When number is an string
*/
class BalanceNumber
{
	is_valid_number(num, size)
	{
		if (num[0] == '0')
		{
			// is start with zero
			// not a valid number
			return false;
		}
		for (var i = 0; i < size; ++i)
		{
			if (!((num[i]).charCodeAt(0) >= ('0').charCodeAt(0) 
                && (num[i]).charCodeAt(0) <= ('9').charCodeAt(0)))
			{
				return false;
			}
		}
		return true;
	}
	//Check whether given number is balanced or not
	balanced_number(num)
	{
		var size = num.length;
		if (size == 0 || this.is_valid_number(num, size) == false)
		{
			return;
		}
		var left = 0;
		var right = 0;
		for (var i = 0; i < parseInt(size / 2); ++i)
		{
			// Calculate sum of left half digits
			left += parseInt(((num[i]).charCodeAt(0) - ('0').charCodeAt(0)));
			// Calculate sum of right half digits
			right += parseInt((((num[size - 1 - i]).charCodeAt(0)) - ('0').charCodeAt(0)));
		}
		if (left == right)
		{
			process.stdout.write(" [" + num + "] Balanced \n");
		}
		else
		{
			process.stdout.write(" [" + num + "] Not Balanced \n");
		}
	}
}

function main()
{
	var obj = new BalanceNumber();
	//Test Case
	obj.balanced_number("1439125");
	obj.balanced_number("131");
	obj.balanced_number("1235");
	obj.balanced_number("24253");
	obj.balanced_number("1230");
	obj.balanced_number("123501");
	obj.balanced_number("1234567895987654321");
}
main();

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
 [1234567895987654321] Balanced
#  Python 3 Program
#  Check if the number is balanced
#  // When number is an string

class BalanceNumber :
	def is_valid_number(self, num, size) :
		if (num[0] == '0') :
			#  is start with zero
			#  not a valid number
			return False
		
		i = 0
		while (i < size) :
			if (not(ord(num[i]) >= ord('0') and ord(num[i]) <= ord('9'))) :
				return False
			
			i += 1
		
		return True
	
	# Check whether given number is balanced or not
	def balanced_number(self, num) :
		size = len(num)
		if (size == 0 or self.is_valid_number(num, size) == False) :
			return
		
		left = 0
		right = 0
		i = 0
		while (i < int(size / 2)) :
			#  Calculate sum of left half digits
			left += int((ord(num[i]) - ord('0')))
			#  Calculate sum of right half digits
			right += int(((ord(num[size - 1 - i])) - ord('0')))
			i += 1
		
		if (left == right) :
			print(" [", num ,"] Balanced \n", end = "")
		else :
			print(" [", num ,"] Not Balanced \n", end = "")
		
	

def main() :
	obj = BalanceNumber()
	# Test Case
	obj.balanced_number("1439125")
	obj.balanced_number("131")
	obj.balanced_number("1235")
	obj.balanced_number("24253")
	obj.balanced_number("1230")
	obj.balanced_number("123501")
	obj.balanced_number("1234567895987654321")

if __name__ == "__main__": main()

Output

 [ 1439125 ] Balanced
 [ 131 ] Balanced
 [ 1235 ] Not Balanced
 [ 24253 ] Not Balanced
 [ 1230 ] Balanced
 [ 123501 ] Balanced
 [ 1234567895987654321 ] Balanced
#   Ruby Program
#   Check if the number is balanced
#   // When number is an string

class BalanceNumber 
	def is_valid_number(num, size) 
		if (num[0] == '0') 
			#  is start with zero
			#  not a valid number
			return false
		end

		i = 0
		while (i < size) 
			if (!((num[i]).ord >= ('0').ord && (num[i]).ord <= ('9').ord)) 
				return false
			end

			i += 1
		end

		return true
	end

	# Check whether given number is balanced or not
	def balanced_number(num) 
		size = num.length()
		if (size == 0 || self.is_valid_number(num, size) == false) 
			return
		end

		left = 0
		right = 0
		i = 0
		while (i < size / 2) 
			#  Calculate sum of left half digits
			left += (((num[i]).ord - ('0').ord)).to_i
			#  Calculate sum of right half digits
			right += ((((num[size - 1 - i]).ord) - ('0').ord)).to_i
			i += 1
		end

		if (left == right) 
			print(" [", num ,"] Balanced \n")
		else 
			print(" [", num ,"] Not Balanced \n")
		end

	end

end

def main() 
	obj = BalanceNumber.new()
	# Test Case
	obj.balanced_number("1439125")
	obj.balanced_number("131")
	obj.balanced_number("1235")
	obj.balanced_number("24253")
	obj.balanced_number("1230")
	obj.balanced_number("123501")
	obj.balanced_number("1234567895987654321")
end

main()

Output

 [1439125] Balanced 
 [131] Balanced 
 [1235] Not Balanced 
 [24253] Not Balanced 
 [1230] Balanced 
 [123501] Balanced 
 [1234567895987654321] Balanced 
/*
  Scala Program
  Check if the number is balanced
  // When number is an string
*/
class BalanceNumber
{
	def is_valid_number(num: String, size: Int): Boolean = {
		if (num(0) == '0')
		{
			// is start with zero
			// not a valid number
			return false;
		}
		var i: Int = 0;
		while (i < size)
		{
			if (!(num(i) >= '0' && num(i) <= '9'))
			{
				return false;
			}
			i += 1;
		}
		return true;
	}
	//Check whether given number is balanced or not
	def balanced_number(num: String): Unit = {
		var size: Int = num.length();
		if (size == 0 || is_valid_number(num, size) == false)
		{
			return;
		}
		var left: Int = 0;
		var right: Int = 0;
		var i: Int = 0;
		while (i < (size / 2).toInt)
		{
			// Calculate sum of left half digits
			left += ((num(i) - '0')).toInt;
			// Calculate sum of right half digits
			right += (((num(size - 1 - i)) - '0')).toInt;
			i += 1;
		}
		if (left == right)
		{
			print(" [" + num + "] Balanced \n");
		}
		else
		{
			print(" [" + num + "] Not Balanced \n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: BalanceNumber = new BalanceNumber();
		//Test Case
		obj.balanced_number("1439125");
		obj.balanced_number("131");
		obj.balanced_number("1235");
		obj.balanced_number("24253");
		obj.balanced_number("1230");
		obj.balanced_number("123501");
		obj.balanced_number("1234567895987654321");
	}
}

Output

 [1439125] Balanced
 [131] Balanced
 [1235] Not Balanced
 [24253] Not Balanced
 [1230] Balanced
 [123501] Balanced
 [1234567895987654321] Balanced
/*
  Swift 4 Program
  Check if the number is balanced
  // When number is an string
*/
class BalanceNumber
{
	func is_valid_number(_ n: String, _ size: Int) -> Bool
	{
      	let num = Array(n);
		if (num[0] == "0")
		{
			// is start with zero
			// not a valid number
			return false;
		}
		var i: Int = 0;
		while (i < size)
		{
			if (!(num[i] >= "0" && num[i] <= "9"))
			{
				return false;
			}
			i += 1;
		}
		return true;
	}
	//Check whether given number is balanced or not
	func balanced_number(_ n: String)
	{	
		let num = Array(n);
		let size: Int = n.count;
		if (size == 0 || self.is_valid_number(n, size) == false)
		{
			return;
		}
		var left: Int = 0;
		var right: Int = 0;
		var i: Int = 0;
		while (i < size / 2)
		{
			// Calculate sum of left half digits
			left += Int(UnicodeScalar(String(num[i]))!.value - 48);
			// Calculate sum of right half digits
			right += Int( UnicodeScalar(String(num[size - 1 - i]))!.value - 48);
			i += 1;
		}
		if (left == right)
		{
			print(" [", n ,"] Balanced \n", terminator: "");
		}
		else
		{
			print(" [", n ,"] Not Balanced \n", terminator: "");
		}
	}
}
func main()
{
	let obj: BalanceNumber = BalanceNumber();
	//Test Case
	obj.balanced_number("1439125");
	obj.balanced_number("131");
	obj.balanced_number("1235");
	obj.balanced_number("24253");
	obj.balanced_number("1230");
	obj.balanced_number("123501");
	obj.balanced_number("1234567895987654321");
}
main();

Output

 [ 1439125 ] Balanced
 [ 131 ] Balanced
 [ 1235 ] Not Balanced
 [ 24253 ] Not Balanced
 [ 1230 ] Balanced
 [ 123501 ] Balanced
 [ 1234567895987654321 ] Balanced


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved