Addition of two numbers by using prefix and postfix

Here given code implementation process.

// C program for 
// Addition of two numbers by using prefix and postfix
#include <stdio.h>

// Addition of given two integers
void sum(int x, int y)
{
	int result = x;
	int remains = y;
	if (result < remains)
	{
		// Select largest of x and y
		result = y;
		// That is small
		remains = x;
	}
	while (remains != 0)
	{
		if (remains < 0)
		{
			// When remains is negative
			result--;
			remains++;
		}
		else
		{
			// When remains is positive
			result++;
			remains--;
		}
	}
	// Display calculated result
	printf(" (%d + %d) = %d \n", x, y, result);
}
int main(int argc, char
	const *argv[])
{
	// Test Case
	sum(-3, -6);
	sum(10, -7);
	sum(-20, 12);
	return 0;
}

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8
/*
  Java Program for
  Addition of two numbers by using prefix and postfix
*/
public class Calculation
{
	// Addition of given two integers
	public void sum(int x, int y)
	{
		int result = x;
		int remains = y;
		if (result < remains)
		{
			// Select largest of x and y
			result = y;
			// That is small
			remains = x;
		}
		while (remains != 0)
		{
			if (remains < 0)
			{
				// When remains is negative
				result--;
				remains++;
			}
			else
			{
				// When remains is positive
				result++;
				remains--;
			}
		}
		// Display calculated result
		System.out.println(" (" + x + " + " + y + ") = " + result);
	}
	public static void main(String[] args)
	{
		Calculation task = new Calculation();
		// Test Case
		task.sum(-3, -6);
		task.sum(10, -7);
		task.sum(-20, 12);
	}
}

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Addition of two numbers by using prefix and postfix
*/
class Calculation
{
	public:
		// Addition of given two integers
		void sum(int x, int y)
		{
			int result = x;
			int remains = y;
			if (result < remains)
			{
				// Select largest of x and y
				result = y;
				// That is small
				remains = x;
			}
			while (remains != 0)
			{
				if (remains < 0)
				{
					// When remains is negative
					result--;
					remains++;
				}
				else
				{
					// When remains is positive
					result++;
					remains--;
				}
			}
			// Display calculated result
			cout << " (" << x << " + " << y << ") = " << result << endl;
		}
};
int main()
{
	Calculation *task = new Calculation();
	// Test Case
	task->sum(-3, -6);
	task->sum(10, -7);
	task->sum(-20, 12);
	return 0;
}

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8
// Include namespace system
using System;
/*
  Csharp Program for
  Addition of two numbers by using prefix and postfix
*/
public class Calculation
{
	// Addition of given two integers
	public void sum(int x, int y)
	{
		int result = x;
		int remains = y;
		if (result < remains)
		{
			// Select largest of x and y
			result = y;
			// That is small
			remains = x;
		}
		while (remains != 0)
		{
			if (remains < 0)
			{
				// When remains is negative
				result--;
				remains++;
			}
			else
			{
				// When remains is positive
				result++;
				remains--;
			}
		}
		// Display calculated result
		Console.WriteLine(" (" + x + " + " + y + ") = " + result);
	}
	public static void Main(String[] args)
	{
		Calculation task = new Calculation();
		// Test Case
		task.sum(-3, -6);
		task.sum(10, -7);
		task.sum(-20, 12);
	}
}

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8
<?php
/*
  Php Program for
  Addition of two numbers by using prefix and postfix
*/
class Calculation
{
	// Addition of given two integers
	public	function sum($x, $y)
	{
		$result = $x;
		$remains = $y;
		if ($result < $remains)
		{
			// Select largest of x and y
			$result = $y;
			// That is small
			$remains = $x;
		}
		while ($remains != 0)
		{
			if ($remains < 0)
			{
				// When remains is negative
				$result--;
				$remains++;
			}
			else
			{
				// When remains is positive
				$result++;
				$remains--;
			}
		}
		// Display calculated result
		echo " (".$x.
		". ".$y.
		") = ".$result.
		"\n";
	}
}

function main()
{
	$task = new Calculation();
	// Test Case
	$task->sum(-3, -6);
	$task->sum(10, -7);
	$task->sum(-20, 12);
}
main();

input

 (-3. -6) = -9
 (10. -7) = 3
 (-20. 12) = -8
/*
  Node JS Program for
  Addition of two numbers by using prefix and postfix
*/
class Calculation
{
	// Addition of given two integers
	sum(x, y)
	{
		var result = x;
		var remains = y;
		if (result < remains)
		{
			// Select largest of x and y
			result = y;
			// That is small
			remains = x;
		}
		while (remains != 0)
		{
			if (remains < 0)
			{
				// When remains is negative
				result--;
				remains++;
			}
			else
			{
				// When remains is positive
				result++;
				remains--;
			}
		}
		// Display calculated result
		console.log(" (" + x + " + " + y + ") = " + result);
	}
}

function main()
{
	var task = new Calculation();
	// Test Case
	task.sum(-3, -6);
	task.sum(10, -7);
	task.sum(-20, 12);
}
main();

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8
#  Python 3 Program for
#  Addition of two numbers by using prefix and postfix
class Calculation :
	#  Addition of given two integers
	def sum(self, x, y) :
		result = x
		remains = y
		if (result < remains) :
			#  Select largest of x and y
			result = y
			#  That is small
			remains = x
		
		while (remains != 0) :
			if (remains < 0) :
				#  When remains is negative
				result -= 1
				remains += 1
			else :
				#  When remains is positive
				result += 1
				remains -= 1
			
		
		#  Display calculated result
		print(" (", x ,"+", y ,") = ", result)
	

def main() :
	task = Calculation()
	#  Test Case
	task.sum(-3, -6)
	task.sum(10, -7)
	task.sum(-20, 12)

if __name__ == "__main__": main()

input

 ( -3 + -6 ) =  -9
 ( 10 + -7 ) =  3
 ( -20 + 12 ) =  -8
#  Ruby Program for
#  Addition of two numbers by using prefix and postfix
class Calculation 
	#  Addition of given two integers
	def sum(x, y) 
		result = x
		remains = y
		if (result < remains) 
			#  Select largest of x and y
			result = y
			#  That is small
			remains = x
		end

		while (remains != 0) 
			if (remains < 0) 
				#  When remains is negative
				result -= 1
				remains += 1
			else 
				#  When remains is positive
				result += 1
				remains -= 1
			end

		end

		#  Display calculated result
		print(" (", x ," + ", y ,") = ", result, "\n")
	end

end

def main() 
	task = Calculation.new()
	#  Test Case
	task.sum(-3, -6)
	task.sum(10, -7)
	task.sum(-20, 12)
end

main()

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8
/*
  Scala Program for
  Addition of two numbers by using prefix and postfix
*/
class Calculation()
{
	// Addition of given two integers
	def sum(x: Int, y: Int): Unit = {
		var result: Int = x;
		var remains: Int = y;
		if (result < remains)
		{
			// Select largest of x and y
			result = y;
			// That is small
			remains = x;
		}
		while (remains != 0)
		{
			if (remains < 0)
			{
				// When remains is negative
				result -= 1;
				remains += 1;
			}
			else
			{
				// When remains is positive
				result += 1;
				remains -= 1;
			}
		}
		// Display calculated result
		println(" (" + x + " + " + y + ") = " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Calculation = new Calculation();
		// Test Case
		task.sum(-3, -6);
		task.sum(10, -7);
		task.sum(-20, 12);
	}
}

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8
/*
  Swift 4 Program for
  Addition of two numbers by using prefix and postfix
*/
class Calculation
{
	// Addition of given two integers
	func sum(_ x: Int, _ y: Int)
	{
		var result: Int = x;
		var remains: Int = y;
		if (result < remains)
		{
			// Select largest of x and y
			result = y;
			// That is small
			remains = x;
		}
		while (remains  != 0)
		{
			if (remains < 0)
			{
				// When remains is negative
				result -= 1;
				remains += 1;
			}
			else
			{
				// When remains is positive
				result += 1;
				remains -= 1;
			}
		}
		// Display calculated result
		print(" (", x ,"+", y ,") = ", result);
	}
}
func main()
{
	let task: Calculation = Calculation();
	// Test Case
	task.sum(-3, -6);
	task.sum(10, -7);
	task.sum(-20, 12);
}
main();

input

 ( -3 + -6 ) =  -9
 ( 10 + -7 ) =  3
 ( -20 + 12 ) =  -8
/*
  Kotlin Program for
  Addition of two numbers by using prefix and postfix
*/
class Calculation
{
	// Addition of given two integers
	fun sum(x: Int, y: Int): Unit
	{
		var result: Int = x;
		var remains: Int = y;
		if (result < remains)
		{
			// Select largest of x and y
			result = y;
			// That is small
			remains = x;
		}
		while (remains != 0)
		{
			if (remains < 0)
			{
				// When remains is negative
				result -= 1;
				remains += 1;
			}
			else
			{
				// When remains is positive
				result += 1;
				remains -= 1;
			}
		}
		// Display calculated result
		println(" (" + x + " + " + y + ") = " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Calculation = Calculation();
	// Test Case
	task.sum(-3, -6);
	task.sum(10, -7);
	task.sum(-20, 12);
}

input

 (-3 + -6) = -9
 (10 + -7) = 3
 (-20 + 12) = -8


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