# Last digit of Product of two Large or Small numbers (a * b)

Given two large or small numbers, the task is to find the last digit of the product of these two numbers.

**Examples:**

Input:a = 1234567891233789, b = 567891233156156Output:4Input:a = 123, b = 456Output:8

**Approach:** In general, the last digit of multiplication of 2 numbers a and b is the last digit of the product of the LSB of these two numbers.

For example: For a = 123 and b = 456,

the last digit of a*b

= Last digit of ((LSB of a)*(LSB of b))

= Last digit of ((3)*(6))

= Last digit of (18)

= 8

Below is the implementation of the above approach:

## C++

`// C++ implementation of the above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Fthe unction to print last digit of product a * b ` `void` `lastDigit(string a, string b) ` `{ ` ` ` `int` `lastDig = (a[a.length() - 1] - ` `'0'` `) ` ` ` `* (b[b.length() - 1] - ` `'0'` `); ` ` ` `cout << lastDig % 10; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string a = ` `"1234567891233"` `, b = ` `"1234567891233156"` `; ` ` ` `lastDigit(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` ` ` `class` `Solution { ` ` ` ` ` `// Function to print last digit of product a * b ` ` ` `public` `static` `void` `lastDigit(String a, String b) ` ` ` `{ ` ` ` `int` `lastDig = (a.charAt(a.length() - ` `1` `) - ` `'0'` `) ` ` ` `* (b.charAt(b.length() - ` `1` `) - ` `'0'` `); ` ` ` `System.out.println(lastDig % ` `10` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `String a = ` `"1234567891233"` `, b = ` `"1234567891233156"` `; ` ` ` `lastDigit(a, b); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function to print the last digit ` `# of product a * b ` `def` `lastDigit(a, b): ` ` ` `lastDig ` `=` `((` `int` `(a[` `len` `(a) ` `-` `1` `]) ` `-` `int` `(` `'0'` `)) ` `*` ` ` `(` `int` `(b[` `len` `(b) ` `-` `1` `]) ` `-` `int` `(` `'0'` `))) ` ` ` `print` `(lastDig ` `%` `10` `) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `a, b ` `=` `"1234567891233"` `, ` `"1234567891233156"` ` ` `lastDigit(a, b) ` ` ` `# This code has been contributed ` `# by 29AjayKumar ` |

*chevron_right*

*filter_none*

## C#

`// CSharp implementation of the above approach ` ` ` `using` `System; ` `class` `Solution { ` ` ` ` ` `// Function to print last digit of product a * b ` ` ` `public` `static` `void` `lastDigit(String a, String b) ` ` ` `{ ` ` ` `int` `lastDig = (a[a.Length - 1] - ` `'0'` `) ` ` ` `* (b[b.Length - 1] - ` `'0'` `); ` ` ` ` ` `Console.Write(lastDig % 10); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `String a = ` `"1234567891233"` `, b = ` `"1234567891233156"` `; ` ` ` `lastDigit(a, b); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## PHP

**Output:**

8

**Time Complexity:** O(1).

## Recommended Posts:

- Find Last Digit of a^b for Large Numbers
- First digit in product of an array of numbers
- Largest palindrome which is product of two n-digit numbers
- Check if all sub-numbers have distinct Digit product
- Check whether a number can be expressed as a product of single digit numbers
- Recursive sum of digit in n^x, where n and x are very large
- Count of Numbers in Range where first digit is equal to last digit of the number
- Digit - Product - Sequence
- Product of N with its largest odd digit
- Count n digit numbers not having a particular digit
- Maximum number with same digit factorial product
- Check if the product of digit sum and its reverse equals the number or not
- Maximum of sum and product of digits until number is reduced to a single digit
- GCD of two numbers when one of them can be very large
- LCM of two large numbers

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.