Posted on by Kalkicode
Code Mathematics

Addition of two numbers by using prefix and postfix

In this article, we will explore the concept of adding two numbers using prefix and postfix operations. We will begin by understanding the problem statement, followed by an explanation of the approach with a suitable example. We will also provide an algorithm and pseudocode along with an explanation, and finally, we will discuss the resultant output explanation with the time complexity of the code.

Introduction

The addition of two numbers is a fundamental operation in mathematics and computer programming. The prefix and postfix notations are alternative ways of representing arithmetic expressions. In prefix notation, also known as Polish notation, the operator is placed before its operands. Conversely, in postfix notation, also known as Reverse Polish notation (RPN), the operator is placed after its operands.

Problem Statement

The problem at hand is to implement a program that adds two numbers using prefix and postfix operations. We are given a C program that performs this addition, and we will analyze it to understand the logic and functioning.

The given C program performs the addition of two integers by using a while loop and conditional statements. The two numbers to be added are passed as parameters to the sum() function. Inside the function, the larger number is stored in the 'result' variable, while the smaller number is stored in the 'remains' variable.

The algorithm follows a simple approach:

1. If 'result' is less than 'remains', swap the values of 'result' and 'remains'.
2. While 'remains' is not equal to zero:
• If 'remains' is negative, decrement 'result' and increment 'remains'.
• If 'remains' is positive, increment 'result' and decrement 'remains'.
3. Display the calculated result.

Example

Let's consider a few test cases to better understand the working of the code:

```Input: (-3 + -6)
Output: (-3 + -6) = -9
Input: (10 + -7)
Output: (10 + -7) = 3

Input: (-20 + 12)
Output: (-20 + 12) = -8
```

In the first test case, (-3 + -6) results in -9. The algorithm correctly performs the addition by incrementing the 'result' and decrementing the 'remains' until 'remains' becomes zero.

Similarly, in the second test case, (10 + -7) results in 3. The algorithm follows the same steps and provides the correct output.

In the third test case, (-20 + 12) results in -8. The algorithm handles negative numbers properly and produces the expected result.

Code Solution

``````// 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
}
}``````

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
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
}
}``````

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
}
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
}
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() :
#  Test Case

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()
#  Test Case
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
}
}``````

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
}
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
}``````

input

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

Time Complexity

The time complexity of the given code is O(N), where N represents the absolute difference between the two input numbers. The while loop runs until 'remains' becomes zero, and the number of iterations depends on the value of 'remains'.

Finally

In this article, we explored the concept of adding two numbers using prefix and postfix operations. We analyzed a given C program that implements this addition and provided a step-by-step explanation of its logic and functioning. Additionally, we discussed a few test cases and explained the resultant output. Finally, we determined the time complexity of the code. Understanding prefix and postfix operations can be helpful in various programming scenarios and can enhance problem-solving skills.

Comment

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.

Categories
Relative Post