Sum of two string numbers
Here given code implementation process.
//C Program
//Sum of two string numbers
#include <stdio.h>
//Add two string numbers
//Assuming that given both strings is valid positive number
void addition(char str1[], char str2[], int s1, int s2)
{
//Loop control variables
//Get the last location
int location1 = s1 - 1;
int location2 = s2 - 1;
//Use to store the result
//Assuming that resultant sum is under 40 characters
//Otherwise increase the size
int result[40];
int index = 0;
int num1 = 0;
int num2 = 0;
int carry = 0;
//Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
{
num1 = 0;
num2 = 0;
if (location1 >= 0)
{
//Get the first number
num1 = str1[location1] - '0';
}
if (location2 >= 0)
{
//Get the second number
num2 = str2[location2] - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result[index] = carry % 10;
carry /= 10;
//get next location
index++;
//modified string text location
location1--;
location2--;
}
//Display String
printf("[%s]+[%s] = ", str1, str2);
for (int i = index - 1; i >= 0; i--)
{
printf("%d", result[i]);
}
printf("\n");
}
int main()
{
char str1[] = "783637";
char str2[] = "12524562";
int size1 = sizeof(str1) / sizeof(str1[0]) - 1;
int size2 = sizeof(str2) / sizeof(str2[0]) - 1;
addition(str1, str2, size1, size2);
//Test Case 2
char str3[] = "98736227467272847";
char str4[] = "355223555";
size1 = sizeof(str3) / sizeof(str3[0]) - 1;
size2 = sizeof(str4) / sizeof(str4[0]) - 1;
addition(str3, str4, size1, size2);
return 0;
}
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
// Java program
// Sum of two string numbers
class MyString
{
//Add two string numbers
//Assuming that given both strings is valid positive number
public void addition(String str1, String str2)
{
//Loop control variables
//Get the last location
int location1 = str1.length() - 1;
int location2 = str2.length() - 1;
//Use to store the result
String result = "";
int index = 0;
int num1 = 0;
int num2 = 0;
int carry = 0;
//Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
{
num1 = 0;
num2 = 0;
if (location1 >= 0)
{
//Get the first number
num1 = str1.charAt(location1) - '0';
}
if (location2 >= 0)
{
//Get the second number
num2 = str2.charAt(location2) - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = carry % 10 + result;
carry /= 10;
//get next location
index++;
//modified string text location
location1--;
location2--;
}
System.out.print("\n[" + str1 + "]+[" + str2 + "] = " + result);
}
public static void main(String[] args)
{
MyString obj = new MyString();
obj.addition("783637", "12524562");
obj.addition("98736227467272847", "355223555");
}
}
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
//Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ program
// Sum of two string numbers
class MyString
{
public:
//Add two string numbers
//Assuming that given both strings is valid positive number
void addition(string str1, string str2)
{
//Loop control variables
//Get the last location
int location1 = str1.size() - 1;
int location2 = str2.size() - 1;
//Use to store the result
string result = "";
int index = 0;
int num1 = 0;
int num2 = 0;
int carry = 0;
//Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
{
num1 = 0;
num2 = 0;
if (location1 >= 0)
{
//Get the first number
num1 = str1[location1] - '0';
}
if (location2 >= 0)
{
//Get the second number
num2 = str2[location2] - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = (to_string( carry % 10)) + result;
carry /= 10;
//get next location
index++;
//modified string text location
location1--;
location2--;
}
cout << "\n[" << str1 << "]+[" << str2 << "] = " << result;
}
};
int main()
{
MyString obj = MyString();
obj.addition("783637", "12524562");
obj.addition("98736227467272847", "355223555");
return 0;
}
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
//Include namespace system
using System;
// C# program
// Sum of two string numbers
class MyString
{
//Add two string numbers
//Assuming that given both strings is valid positive number
public void addition(String str1, String str2)
{
//Loop control variables
//Get the last location
int location1 = str1.Length - 1;
int location2 = str2.Length - 1;
//Use to store the result
String result = "";
int index = 0;
int num1 = 0;
int num2 = 0;
int carry = 0;
//Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
{
num1 = 0;
num2 = 0;
if (location1 >= 0)
{
//Get the first number
num1 = str1[location1] - '0';
}
if (location2 >= 0)
{
//Get the second number
num2 = str2[location2] - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = carry % 10 + result;
carry /= 10;
//get next location
index++;
//modified string text location
location1--;
location2--;
}
Console.Write("\n[" + str1 + "]+[" + str2 + "] = " + result);
}
public static void Main(String[] args)
{
MyString obj = new MyString();
obj.addition("783637", "12524562");
obj.addition("98736227467272847", "355223555");
}
}
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
<?php
// Php program
// Sum of two string numbers
class MyString
{
//Add two string numbers
//Assuming that given both strings is valid positive number
public function addition($str1, $str2)
{
//Loop control variables
//Get the last location
$location1 = strlen($str1) - 1;
$location2 = strlen($str2) - 1;
//Use to store the result
$result = "";
$index = 0;
$num1 = 0;
$num2 = 0;
$carry = 0;
//Execute loop to get both string number
while ($location1 >= 0 || $location2 >= 0 || $carry > 0)
{
$num1 = 0;
$num2 = 0;
if ($location1 >= 0)
{
//Get the first number
$num1 = ord($str1[$location1]) - ord('0');
}
if ($location2 >= 0)
{
//Get the second number
$num2 = ord($str2[$location2]) - ord('0');
}
$carry = $carry + $num1 + $num2;
//add sum into the result
$result = ($carry % 10) . $result;
$carry = intval($carry / 10);
//get next location
$index++;
//modified string text location
$location1--;
$location2--;
}
echo "\n[". $str1 ."]+[". $str2 ."] = ". $result;
}
}
function main()
{
$obj = new MyString();
$obj->addition("783637", "12524562");
$obj->addition("98736227467272847", "355223555");
}
main();
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
// Node Js program
// Sum of two string numbers
class MyString
{
//Add two string numbers
//Assuming that given both strings is valid positive number
addition(str1, str2)
{
//Loop control variables
//Get the last location
var location1 = str1.length - 1;
var location2 = str2.length - 1;
//Use to store the result
var result = "";
var index = 0;
var num1 = 0;
var num2 = 0;
var carry = 0;
//Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
{
num1 = 0;
num2 = 0;
if (location1 >= 0)
{
//Get the first number
num1 = str1[location1] - '0';
}
if (location2 >= 0)
{
//Get the second number
num2 = str2[location2] - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = carry % 10 + result;
carry = parseInt(carry / 10);
//get next location
index++;
//modified string text location
location1--;
location2--;
}
process.stdout.write("\n[" + str1 + "]+[" + str2 + "] = " + result);
}
}
function main()
{
var obj = new MyString();
obj.addition("783637", "12524562");
obj.addition("98736227467272847", "355223555");
}
main();
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
# Python 3 program
# Sum of two string numbers
class MyString :
# Add two string numbers
# Assuming that given both strings is valid positive number
def addition(self, str1, str2) :
# Loop control variables
# Get the last location
location1 = len(str1) - 1
location2 = len(str2) - 1
# Use to store the result
result = ""
index = 0
num1 = 0
num2 = 0
carry = 0
# Execute loop to get both string number
while (location1 >= 0 or location2 >= 0 or carry > 0) :
num1 = 0
num2 = 0
if (location1 >= 0) :
# Get the first number
num1 = ord(str1[location1]) - ord('0')
if (location2 >= 0) :
# Get the second number
num2 = ord(str2[location2]) - ord('0')
carry = carry + num1 + num2
# add sum into the result
result = str(carry % 10) + result
carry = int(carry / 10)
# get next location
index += 1
# modified string text location
location1 -= 1
location2 -= 1
print("\n[", str1 ,"]+[", str2 ,"] = ", result, end = "")
def main() :
obj = MyString()
obj.addition("783637", "12524562")
obj.addition("98736227467272847", "355223555")
if __name__ == "__main__": main()
Output
[ 783637 ]+[ 12524562 ] = 13308199
[ 98736227467272847 ]+[ 355223555 ] = 98736227822496402
# Ruby program
# Sum of two string numbers
class MyString
# Add two string numbers
# Assuming that given both strings is valid positive number
def addition(str1, str2)
# Loop control variables
# Get the last location
location1 = str1.length() - 1
location2 = str2.length() - 1
# Use to store the result
result = ""
index = 0
num1 = 0
num2 = 0
carry = 0
# Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
num1 = 0
num2 = 0
if (location1 >= 0)
# Get the first number
num1 = str1[location1].ord - ('0').ord
end
if (location2 >= 0)
# Get the second number
num2 = str2[location2].ord - ('0').ord
end
carry = carry + num1 + num2
# add sum into the result
result = (carry % 10).to_s + result
carry = carry / 10
# get next location
index += 1
# modified string text location
location1 -= 1
location2 -= 1
end
print("\n[", str1 ,"]+[", str2 ,"] = ", result)
end
end
def main()
obj = MyString.new()
obj.addition("783637", "12524562")
obj.addition("98736227467272847", "355223555")
end
main()
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
// Scala program
// Sum of two string numbers
class MyString
{
//Add two string numbers
//Assuming that given both strings is valid positive number
def addition(str1: String, str2: String): Unit = {
//Loop control variables
//Get the last location
var location1: Int = str1.length() - 1;
var location2: Int = str2.length() - 1;
//Use to store the result
var result: String = "";
var index: Int = 0;
var num1: Int = 0;
var num2: Int = 0;
var carry: Int = 0;
//Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
{
num1 = 0;
num2 = 0;
if (location1 >= 0)
{
//Get the first number
num1 = str1(location1) - '0';
}
if (location2 >= 0)
{
//Get the second number
num2 = str2(location2) - '0';
}
carry = carry + num1 + num2;
//add sum into the result
result = "" + (carry % 10) + result;
carry = (carry / 10).toInt;
//get next location
index += 1;
//modified string text location
location1 -= 1;
location2 -= 1;
}
print("\n[" + str1 + "]+[" + str2 + "] = " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
obj.addition("783637", "12524562");
obj.addition("98736227467272847", "355223555");
}
}
Output
[783637]+[12524562] = 13308199
[98736227467272847]+[355223555] = 98736227822496402
// Swift program
// Sum of two string numbers
class MyString
{
//Add two string numbers
//Assuming that given both strings is valid positive number
func addition(_ text1: String, _ text2: String)
{
var str1 = Array(text1);
var str2 = Array(text2);
//Loop control variables
//Get the last location
var location1: Int = str1.count - 1;
var location2: Int = str2.count - 1;
//Use to store the result
var result: String = "";
var index: Int = 0;
var num1: Int = 0;
var num2: Int = 0;
var carry: Int = 0;
//Execute loop to get both string number
while (location1 >= 0 || location2 >= 0 || carry > 0)
{
num1 = 0;
num2 = 0;
if (location1 >= 0)
{
//Get the first number
num1 = Int(UnicodeScalar(String(str1[location1]))!.value - UnicodeScalar("0")!.value);
}
if (location2 >= 0)
{
//Get the second number
num2 = Int(UnicodeScalar(String(str2[location2]))!.value - UnicodeScalar("0")!.value);
}
carry = carry + num1 + num2;
//add sum into the result
result = String(carry % 10) + result;
carry = carry / 10;
//get next location
index += 1;
//modified string text location
location1 -= 1;
location2 -= 1;
}
print("\n[", text1 ,"]+[", text2 ,"] = ", result, terminator: "");
}
}
func main()
{
let obj: MyString = MyString();
obj.addition("783637", "12524562");
obj.addition("98736227467272847", "355223555");
}
main();
Output
[ 783637 ]+[ 12524562 ] = 13308199
[ 98736227467272847 ]+[ 355223555 ] = 98736227822496402
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