Sum of two binary strings

Here given code implementation process.

//C++ Program
//Addition of two binary string
#include <iostream>

using namespace std;
class MyString
{
	public:
		//Adding the result of given binary text data
		void addition(string text1, string text2)
		{
			//First get the length of binary text string
			int size1 = text1.size();
			int size2 = text2.size();
			int counter = size1;
			if (counter < size2)
			{
				//When given second binary text length is greater than of first
				counter = size2;
			}
			//Variable which is collect the sum of given binary text
			int result = 0;
			int submit = 1;
			//variable use to check valid binary tree text
			bool validate = true;
			for (int i = 1; i <= counter && (validate == true); ++i)
			{
				if (i <= size1)
				{
					validate = false;
					if (text1[size1 - i] == '1')
					{
						result += submit;
						validate = true;
					}
					if (text1[size1 - i] == '0')
					{
						//When get a valid binary bit
						validate = true;
					}
				}
				if (i <= size2)
				{
					validate = false;
					if (text2[size2 - i] == '1')
					{
						result += submit;
						validate = true;
					}
					if (text2[size2 - i] == '0')
					{
						//When get a valid binary bit
						validate = true;
					}
				}
				//increase power 2^0, 2^1, 2^2 ...
				submit += submit;
			}
			cout << " Addition of binary [" << text1 << " + " << text2 << " ]";
			if (validate == true)
			{
				cout << " : " << result << endl;
			}
			else
			{
				//When find invalid binary text
				cout << " : None [Invalid Binary]" << endl;
			}
		}
};
int main()
{
	MyString obj;
	//Assign the binary text value to string variable	
	string text1 = "00111";
	string text2 = "0011100";
	obj.addition(text1, text2);
	text1 = "1000000";
	text2 = "01010";
	obj.addition(text1, text2);
	text1 = "1000000";
	text2 = "05010";
	obj.addition(text1, text2);
	return 0;
}

Output

 Addition of binary [00111 + 0011100 ] : 35
 Addition of binary [1000000 + 01010 ] : 74
 Addition of binary [1000000 + 05010 ] : None [Invalid Binary]
/*
Java program
Addition of two binary string
*/
class MyString
{
	//Adding the result of given binary text data
	public void addition(String text1, String text2)
	{
		//First get the length of binary text String
		int size1 = text1.length();
		int size2 = text2.length();
		int counter = size1;
		if (counter < size2)
		{
			//When given second binary text length is greater than of first
			counter = size2;
		}
		//Variable which is collect the sum of given binary text
		int result = 0;
		int submit = 1;
		//variable use to check valid binary tree text
		boolean validate = true;
		for (int i = 1; i <= counter && (validate == true); ++i)
		{
			if (i <= size1)
			{
				validate = false;
				if (text1.charAt(size1 - i) == '1')
				{
					result += submit;
					validate = true;
				}
				if (text1.charAt(size1 - i) == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			if (i <= size2)
			{
				validate = false;
				if (text2.charAt(size2 - i) == '1')
				{
					result += submit;
					validate = true;
				}
				if (text2.charAt(size2 - i) == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			//increase power 2^0, 2^1, 2^2 ...
			submit += submit;
		}
		System.out.print(" Addition of binary [" + text1 + " + " + text2 + " ]");
		if (validate == true)
		{
			System.out.print(" : " + result);
		}
		else
		{
			//When find invalid binary text
			System.out.print(" : None [Invalid Binary]");
		}
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		//Assign the binary text value to String variable	
		String text1 = "00111";
		String text2 = "0011100";
		obj.addition(text1, text2);
		text1 = "1000000";
		text2 = "01010";
		obj.addition(text1, text2);
		text1 = "1000000";
		text2 = "05010";
		obj.addition(text1, text2);
	}
}

Output

 Addition of binary [00111 + 0011100 ] : 35 Addition of binary [1000000 + 01010 ] : 74 Addition of binary [1000000 + 05010 ] : None [Invalid Binary]
/*
C# program
Addition of two binary string
*/
using System;
class MyString
{
	//Adding the result of given binary text data
	public void addition(String text1, String text2)
	{
		//First get the length of binary text String
		int size1 = text1.Length;
		int size2 = text2.Length;
		int counter = size1;
		if (counter < size2)
		{
			//When given second binary text length is greater than of first
			counter = size2;
		}
		//Variable which is collect the sum of given binary text
		int result = 0;
		int submit = 1;
		//variable use to check valid binary tree text
		Boolean validate = true;
		for (int i = 1; i <= counter && (validate == true); ++i)
		{
			if (i <= size1)
			{
				validate = false;
				if (text1[size1 - i] == '1')
				{
					result += submit;
					validate = true;
				}
				if (text1[size1 - i] == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			if (i <= size2)
			{
				validate = false;
				if (text2[size2 - i] == '1')
				{
					result += submit;
					validate = true;
				}
				if (text2[size2 - i] == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			//increase power 2^0, 2^1, 2^2 ...
			submit += submit;
		}
		Console.Write(" Addition of binary [" + text1 + " + " + text2 + " ]");
		if (validate == true)
		{
			Console.Write(" : " + result);
		}
		else
		{
			//When find invalid binary text
			Console.Write(" : None [Invalid Binary]");
		}
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		//Assign the binary text value to String variable	
		String text1 = "00111";
		String text2 = "0011100";
		obj.addition(text1, text2);
		text1 = "1000000";
		text2 = "01010";
		obj.addition(text1, text2);
		text1 = "1000000";
		text2 = "05010";
		obj.addition(text1, text2);
	}
}

Output

 Addition of binary [00111 + 0011100 ] : 35 Addition of binary [1000000 + 01010 ] : 74 Addition of binary [1000000 + 05010 ] : None [Invalid Binary]
<?php
/*
Php program
Addition of two binary string
*/
class MyString
{
	//Adding the result of given binary text data
	public	function addition($text1, $text2)
	{
		//First get the length of binary text String
		$size1 = strlen($text1);
		$size2 = strlen($text2);
		$counter = $size1;
		if ($counter < $size2)
		{
			//When given second binary text length is greater than of first
			$counter = $size2;
		}
		//Variable which is collect the sum of given binary text
		$result = 0;
		$submit = 1;
		//variable use to check valid binary tree text
		$validate = true;
		for ($i = 1; $i <= $counter && ($validate == true); ++$i)
		{
			if ($i <= $size1)
			{
				$validate = false;
				if ($text1[$size1 - $i] == '1')
				{
					$result += $submit;
					$validate = true;
				}
				if ($text1[$size1 - $i] == '0')
				{
					//When get a valid binary bit
					$validate = true;
				}
			}
			if ($i <= $size2)
			{
				$validate = false;
				if ($text2[$size2 - $i] == '1')
				{
					$result += $submit;
					$validate = true;
				}
				if ($text2[$size2 - $i] == '0')
				{
					//When get a valid binary bit
					$validate = true;
				}
			}
			//increase power 2^0, 2^1, 2^2 ...
			$submit += $submit;
		}
		echo " Addition of binary [". $text1 .". ". $text2 ." ]";
		if ($validate == true)
		{
			echo " : ". $result;
		}
		else
		{
			echo " : None [Invalid Binary]";
		}
	}
}

function main()
{
	$obj = new MyString();
	//Assign the binary text value to String variable	
	$text1 = "00111";
	$text2 = "0011100";
	$obj->addition($text1, $text2);
	$text1 = "1000000";
	$text2 = "01010";
	$obj->addition($text1, $text2);
	$text1 = "1000000";
	$text2 = "05010";
	$obj->addition($text1, $text2);
}
main();

Output

 Addition of binary [00111. 0011100 ] : 35 Addition of binary [1000000. 01010 ] : 74 Addition of binary [1000000. 05010 ] : None [Invalid Binary]
/*
Node Js program
Addition of two binary string
*/
class MyString
{
	//Adding the result of given binary text data
	addition(text1, text2)
	{
		//First get the length of binary text String
		var size1 = text1.length;
		var size2 = text2.length;
		var counter = size1;
		if (counter < size2)
		{
			//When given second binary text length is greater than of first
			counter = size2;
		}
		//Variable which is collect the sum of given binary text
		var result = 0;
		var submit = 1;
		//variable use to check valid binary tree text
		var validate = true;
		for (var i = 1; i <= counter && (validate == true); ++i)
		{
			if (i <= size1)
			{
				validate = false;
				if (text1[size1 - i] == '1')
				{
					result += submit;
					validate = true;
				}
				if (text1[size1 - i] == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			if (i <= size2)
			{
				validate = false;
				if (text2[size2 - i] == '1')
				{
					result += submit;
					validate = true;
				}
				if (text2[size2 - i] == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			//increase power 2^0, 2^1, 2^2 ...
			submit += submit;
		}
		process.stdout.write(" Addition of binary [" + text1 + " + " + text2 + " ]");
		if (validate == true)
		{
			process.stdout.write(" : " + result);
		}
		else
		{
			process.stdout.write(" : None [Invalid Binary]");
		}
	}
}

function main()
{
	var obj = new MyString();
	//Assign the binary text value to String variable	
	var text1 = "00111";
	var text2 = "0011100";
	obj.addition(text1, text2);
	text1 = "1000000";
	text2 = "01010";
	obj.addition(text1, text2);
	text1 = "1000000";
	text2 = "05010";
	obj.addition(text1, text2);
}
main();

Output

 Addition of binary [00111 + 0011100 ] : 35 Addition of binary [1000000 + 01010 ] : 74 Addition of binary [1000000 + 05010 ] : None [Invalid Binary]
# 
# Python 3 program
# Addition of two binary string

class MyString :
	# Adding the result of given binary text data
	def addition(self, text1, text2) :
		# First get the length of binary text String
		size1 = len(text1)
		size2 = len(text2)
		counter = size1
		if (counter < size2) :
			# When given second binary text length is greater than of first
			counter = size2
		
		# Variable which is collect the sum of given binary text
		result = 0
		submit = 1
		# variable use to check valid binary tree text
		validate = True
		i = 1
		while (i <= counter and(validate == True)) :
			if (i <= size1) :
				validate = False
				if (text1[size1 - i] == '1') :
					result += submit
					validate = True
				
				if (text1[size1 - i] == '0') :
					# When get a valid binary bit
					validate = True
				
			
			if (i <= size2) :
				validate = False
				if (text2[size2 - i] == '1') :
					result += submit
					validate = True
				
				if (text2[size2 - i] == '0') :
					# When get a valid binary bit
					validate = True
				
			
			# increase power 2^0, 2^1, 2^2 ...
			submit += submit
			i += 1
		
		print("\n Addition of binary [", text1 ,", ", text2 ," ]", end = "")
		if (validate == True) :
			print(" : ", result, end = "")
		else :
			print(" : None [Invalid Binary]", end = "")
		
	

def main() :
	obj = MyString()
	# Assign the binary text value to String variable	
	text1 = "00111"
	text2 = "0011100"
	obj.addition(text1, text2)
	text1 = "1000000"
	text2 = "01010"
	obj.addition(text1, text2)
	text1 = "1000000"
	text2 = "05010"
	obj.addition(text1, text2)

if __name__ == "__main__": main()

Output

 Addition of binary [ 00111 ,  0011100  ] :  35
 Addition of binary [ 1000000 ,  01010  ] :  74
 Addition of binary [ 1000000 ,  05010  ] : None [Invalid Binary]
# Ruby program
# Addition of two binary string

class MyString

	# Adding the result of given binary text data
	def addition(text1, text2)
	
		# First get the length of binary text String
		size1 = text1.length()
		size2 = text2.length()
		counter = size1
		if (counter < size2)
		
			# When given second binary text length is greater than of first
			counter = size2
		end
		# Variable which is collect the sum of given binary text
		result = 0
		submit = 1
		# variable use to check valid binary tree text
		validate = true
		i = 1
		while (i <= counter && (validate == true))
		
			if (i <= size1)
			
				validate = false
				if (text1[size1 - i] == '1')
				
					result += submit
					validate = true
				end
				if (text1[size1 - i] == '0')
				
					# When get a valid binary bit
					validate = true
				end
			end
			if (i <= size2)
			
				validate = false
				if (text2[size2 - i] == '1')
				
					result += submit
					validate = true
				end
				if (text2[size2 - i] == '0')
				
					# When get a valid binary bit
					validate = true
				end
			end
			# increase power 2^0, 2^1, 2^2 ...
			submit += submit
			i += 1
		end
		print("\n Addition of binary [", text1 ,", ", text2 ," ]")
		if (validate == true)
		
			print(" : ", result)
		else
		
			# When find invalid binary text
			print(" : None [Invalid Binary]")
		end
	end
end
def main()

	obj = MyString.new()
	# Assign the binary text value to String variable	
	text1 = "00111"
	text2 = "0011100"
	obj.addition(text1, text2)
	text1 = "1000000"
	text2 = "01010"
	obj.addition(text1, text2)
	text1 = "1000000"
	text2 = "05010"
	obj.addition(text1, text2)
end
main()

Output

 Addition of binary [00111, 0011100 ] : 35
 Addition of binary [1000000, 01010 ] : 74
 Addition of binary [1000000, 05010 ] : None [Invalid Binary]
/*
Scala program
Addition of two binary string
*/
class MyString
{
	//Adding the result of given binary text data
	def addition(text1: String, text2: String): Unit = {
		//First get the length of binary text String
		var size1: Int = text1.length();
		var size2: Int = text2.length();
		var counter: Int = size1;
		if (counter < size2)
		{
			//When given second binary text length is greater than of first
			counter = size2;
		}
		//Variable which is collect the sum of given binary text
		var result: Int = 0;
		var submit: Int = 1;
		//variable use to check valid binary tree text
		var validate: Boolean = true;
		var i: Int = 1;
		while (i <= counter && (validate == true))
		{
			if (i <= size1)
			{
				validate = false;
				if (text1(size1 - i) == '1')
				{
					result += submit;
					validate = true;
				}
				if (text1(size1 - i) == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			if (i <= size2)
			{
				validate = false;
				if (text2(size2 - i) == '1')
				{
					result += submit;
					validate = true;
				}
				if (text2(size2 - i) == '0')
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			//increase power 2^0, 2^1, 2^2 ...
			submit += submit;
			i += 1;
		}
		print("\n Addition of binary [" + text1 + " + " + text2 + " ]");
		if (validate == true)
		{
			print(" : " + result);
		}
		else
		{
			//When find invalid binary text
			print(" : None [Invalid Binary]");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		//Assign the binary text value to String variable	
		var text1: String = "00111";
		var text2: String = "0011100";
		obj.addition(text1, text2);
		text1 = "1000000";
		text2 = "01010";
		obj.addition(text1, text2);
		text1 = "1000000";
		text2 = "05010";
		obj.addition(text1, text2);
	}
}

Output

 Addition of binary [00111 + 0011100 ] : 35
 Addition of binary [1000000 + 01010 ] : 74
 Addition of binary [1000000 + 05010 ] : None [Invalid Binary]
/*
Swift program
Addition of two binary string
*/
class MyString
{
	//Adding the result of given binary text data
	func addition(_ t1: String, _ t2: String)
	{
      	let text1 = Array(t1);
      	let text2 = Array(t2);
		//First get the length of binary text String
		let size1: Int = text1.count;
		let size2: Int = text2.count;
      
		var counter: Int = size1;
		if (counter < size2)
		{
			//When given second binary text length is greater than of first
			counter = size2;
		}
		//Variable which is collect the sum of given binary text
		var result: Int = 0;
		var submit: Int = 1;
		//variable use to check valid binary tree text
		var validate: Bool = true;
		var i: Int = 1;
		while (i <= counter && (validate == true))
		{
			if (i <= size1)
			{
				validate = false;
				if (text1[size1 - i] == "1")
				{
					result += submit;
					validate = true;
				}
				if (text1[size1 - i] == "0")
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			if (i <= size2)
			{
				validate = false;
				if (text2[size2 - i] == "1")
				{
					result += submit;
					validate = true;
				}
				if (text2[size2 - i] == "0")
				{
					//When get a valid binary bit
					validate = true;
				}
			}
			//increase power 2^0, 2^1, 2^2 ...
			submit += submit;
			i += 1;
		}
		print("\n Addition of binary [", t1 ,", ", t2 ," ]", terminator: "");
		if (validate == true)
		{
			print(" : ", result, terminator: "");
		}
		else
		{
			print(" : None [Invalid Binary]", terminator: "");
		}
	}
}
func main()
{
	let obj: MyString = MyString();
	//Assign the binary text value to String variable	
	var text1: String = "00111";
	var text2: String = "0011100";
	obj.addition(text1, text2);
	text1 = "1000000";
	text2 = "01010";
	obj.addition(text1, text2);
	text1 = "1000000";
	text2 = "05010";
	obj.addition(text1, text2);
}
main();

Output

 Addition of binary [ 00111 ,  0011100  ] :  35
 Addition of binary [ 1000000 ,  01010  ] :  74
 Addition of binary [ 1000000 ,  05010  ] : None [Invalid Binary]


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