Given three integers N, A, and B. A person is standing at 0-th coordinate and moves A steps to the right in the first step, B steps to the left at the second step, and so on. The task is to find out at which coordinate he will be after N steps. **Examples:**

Input:N = 3, A = 5 and B = 2Output:8

5 to the right, 2 to the left and 5 to the right, hence the person will end at 8.Input:N = 5, A = 1 and B = 10Output:-17

**Approach:** Since the person takes the odd number step to right and even the number of steps to the left, we have to find out the number difference in steps in either direction. Hence, the formula obtained will be thus:

[((n+1)/2)*a - (n/2)*b]

Below is the implementation of the above approach:

## C++

`// C++ program to find the last coordinate` `// where it ends his journey` `#include <bits/stdc++.h>` `using` `namespace` `std;` ` ` `// Function to return the last destination` `int` `lastCoordinate(` `int` `n, ` `int` `a, ` `int` `b)` `{` ` ` `return` `((n + 1) / 2) * a - (n / 2) * b;` `}` ` ` `// Driver Code` `int` `main()` `{` ` ` `int` `n = 3, a = 5, b = 2;` ` ` `cout << lastCoordinate(n, a, b);` ` ` ` ` `return` `0;` `}` |

## Java

`// Java program to find the last coordinate` `// where it ends his journey` `import` `java.util.*;` ` ` `class` `solution` `{` ` ` `// Function to return the last destination` `static` `int` `lastCoordinate(` `int` `n, ` `int` `a, ` `int` `b)` `{` ` ` `return` `((n + ` `1` `) / ` `2` `) * a - (n / ` `2` `) * b;` `}` ` ` `// Driver Code` `public` `static` `void` `main(String args[])` `{` ` ` `int` `n = ` `3` `, a = ` `5` `, b = ` `2` `;` ` ` `System.out.println(lastCoordinate(n, a, b));` ` ` `}` `}` |

## Python

`# Python3 program to find the ` `# last coordinate where it` `# ends his journey ` ` ` `# Function to return the` `# last destination ` `def` `lastCoordinate(n, a, b):` ` ` `return` `(((n ` `+` `1` `) ` `/` `/` `2` `) ` `*` ` ` `a ` `-` `(n ` `/` `/` `2` `) ` `*` `b)` ` ` `# Driver Code ` `n ` `=` `3` `a ` `=` `5` `b ` `=` `2` ` ` `print` `(lastCoordinate(n, a, b))` ` ` `# This code is contributed` `# by Sanjit_Prasad` |

## C#

`// C# program to find the last coordinate ` `// where it ends his journey ` `using` `System;` ` ` `class` `GFG` `{` ` ` `// Function to return the last destination ` `public` `static` `int` `lastCoordinate(` `int` `n, ` ` ` `int` `a, ` `int` `b)` `{` ` ` `return` `((n + 1) / 2) * a - (n / 2) * b;` `}` ` ` `// Driver Code ` `public` `static` `void` `Main(` `string` `[] args)` `{` ` ` `int` `n = 3, a = 5, b = 2;` ` ` `Console.WriteLine(lastCoordinate(n, a, b));` `}` `}` ` ` `// This code is contributed by Shrikant13` |

## PHP

`<?php` `// PHP program to find the last coordinate` `// where it ends his journey` ` ` `// Function to return the last destination` `function` `lastCoordinate(` `$n` `, ` `$a` `, ` `$b` `)` `{` ` ` `return` `((` `$n` `+ 1) / 2) * ` `$a` `- ` ` ` `(int)(` `$n` `/ 2) * ` `$b` `;` `}` ` ` `// Driver Code` `$n` `= 3; ` `$a` `= 5; ` `$b` `= 2;` `echo` `lastCoordinate(` `$n` `, ` `$a` `, ` `$b` `);` ` ` `// This code is contributed by inder_verma..` `?>` |

## Javascript

`<script>` ` ` `// Javascript program to find` `// the last coordinate` `// where it ends his journey` ` ` `// Function to return the last destination` `function` `lastCoordinate(n, a, b)` `{` ` ` `return` `(parseInt(n + 1) / 2) * a - ` ` ` `parseInt(n / 2) * b;` `}` ` ` `// Driver Code` ` ` `let n = 3, a = 5, b = 2;` ` ` `document.write(lastCoordinate(n, a, b));` ` ` `</script>` |

**Output:**

8

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**