Program to find the last digit of X in base Y Last Updated : 20 Dec, 2022 Comments Improve Suggest changes Like Article Like Report 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 = 7 Output: 3 10 is 13 in base 9 with last digit 3 Input: X = 55, Y = 3 Output: 1 55 is 3 in base 601 with last digit 1 Approach: When we try to convert X into the base YWe repeatedly divide X by base Y and store the remainder.So 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 rThen the resultant number in base Y will be rqpAnd the last digit will be pTherefore, we just need to find the first remainder of X when divided by Y to get the last 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; } Java // Java Program to find // the last digit of X in base Y import java.io.*; public 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 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 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 PHP <?php // PHP Program to find the last digit // of X in base Y // Function to find the last // digit of X in base Y function last_digit($X,$Y){ echo( $X % $Y ); } // Driver code $X = 55; $Y = 3; last_digit($X,$Y); ?> JavaScript <script> // Javascript Program to find // the last digit of X in base Y // Function to find the last // digit of X in base Y function last_digit(X, Y) { document.write(X % Y); } // Driver code var X = 55, Y = 3; last_digit(X, Y); </script> Output: 1 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to find the last digit of X in base Y S spp____ Follow Improve Article Tags : Mathematical Computer Science Fundamentals DSA number-digits Aptitude +1 More Practice Tags : Mathematical Similar Reads Program to find the last two digits of x^y The task is to find the last two digits of x^y. Since the digits with which it can end are 0-9, Hence this problem can be divided into 5 cases: Case 1: when x ends with 1 For finding the last two digit of a number, when the number ends with 1 then we have to do following steps shown as in the figure 6 min read Program to find last two digits of 2^n Given a number n, we need to find the last two digits of 2n. Examples: Input : n = 7 Output : 28 Input : n = 72 Output : 96 2^72 = 4722366482869645213696 A Naive Approach is to find the value of 2^n iteratively or using pow function. Once the value of 2^n is calculated, find the last two digits and 15 min read Program to add two integers of given base Given three integers X, Y, and B, where X and Y are Base-B integers. The task is to find the sum of integers X and Y. Examples: Input: X = 123, Y = 234, B = 6 Output: 401 Explanation: Sum of two integers in base 6 - 1 1 1 2 3 + 2 3 4 ------------- 4 0 1 Input: X = 546, Y = 248 B = 9 Output: 805 Expl 9 min read Program to Subtract two integers of given base Given three positive integers X, Y, and B, where X and Y are Base-B integers, the task is to find the value of X - Y such that X >= Y. Examples: Input: X = 1212, Y = 256, B = 8Output: 0734Explanation: The value of 1212 - 256 in base 8 is 734. Input: X = 546, Y = 248, B = 9Output: 287 Approach: Th 12 min read Find unit digit of x raised to power y Given two numbers x and y, find unit digit of xy. Examples : Input : x = 2, y = 1 Output : 2 Explanation 2^1 = 2 so units digit is 2. Input : x = 4, y = 2 Output : 6 Explanation 4^2 = 16 so units digit is 6. Method 1 (Simple) Compute value of xy and find its last digit. This method causes overflow f 11 min read Like