Given a positive integer X and Y, the task is to find the last digit of X in the given base Y.

**Examples:**

Input:X = 10, Y = 7Output:3 10 is 13 in base 9 with last digit 3Input:X = 55, Y = 3Output:1 55 is 3 in base 601 with last digit 1

**Approach:**

- When we try to convert X into the base Y
- We repeatedly divide X by base Y and store the remainder.
- So the final result comprises of the remainders in the order of division steps.
- Lets say the remainder of division step 1 is p, step 2 is q, step 3 is r
- Then the resultant number in base Y will be rqp
- And the last digit will be p
- Therefore, we just need to find the first remainder of X when divided by Y to get the lsat digit in X in base Y.
last digit = X % Y

Below is the implementation of the above approach:

## C++

`// C++ Program to find ` `// the last digit of X in base Y ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the last ` `// digit of X in base Y ` `void` `last_digit(` `int` `X, ` `int` `Y) ` `{ ` ` ` `cout << X % Y; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `X = 55, Y = 3; ` ` ` `last_digit(X, Y); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find ` `// the last digit of X in base Y ` `class` `GFG ` `{ ` ` ` `// Function to find the last ` `// digit of X in base Y ` `static` `void` `last_digit(` `int` `X, ` `int` `Y) ` `{ ` ` ` `System.out.print(X % Y); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String []args) ` `{ ` ` ` `int` `X = ` `55` `, Y = ` `3` `; ` ` ` `last_digit(X, Y); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find ` `# the last digit of X in base Y ` ` ` `# Function to find the last ` `# digit of X in base Y ` `def` `last_digit(X, Y) : ` ` ` ` ` `print` `(X ` `%` `Y); ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `X ` `=` `55` `; Y ` `=` `3` `; ` ` ` `last_digit(X, Y); ` ` ` `# This code is contributed ` `# by AnkitRai01 ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find the last digit ` `// of X in base Y ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the last ` `// digit of X in base Y ` `static` `void` `last_digit(` `int` `X, ` `int` `Y) ` `{ ` ` ` `Console.Write(X % Y); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `int` `X = 55, Y = 3; ` ` ` `last_digit(X, Y); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

1

**Time Complexity:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the Nth digit from right in base B of the given number in Decimal base
- C++ program to find all numbers less than n, which are palindromic in base 10 and base 2.
- Find the Largest N digit perfect square number in Base B
- Given a number N in decimal base, find the sum of digits in any base B
- Given a number N in decimal base, find number of its digits in any base (base b)
- Largest Even and Odd N-digit numbers of base B
- Largest N digit number in Base B
- Largest value of x such that ax
^{x}is N-digit number of base b - Count of N-digit numbers in base K with no two consecutive zeroes
- Program to find last digit of n'th Fibonnaci Number
- Find the remainder when First digit of a number is divided by its Last digit
- Program to compute log a to any base b (log
_{b}a) - Program to add two integers of given base
- Write a program to add two numbers in base 14
- Find most significant bit of a number X in base Y
- Add two integers of different base and represent sum in smaller base of the two
- Find the length of factorial of a number in any given base
- Find the total count of numbers up to N digits in a given base B
- Find the height of a right-angled triangle whose area is X times its base
- Program for replacing one digit with other

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.