Given a number N, find the remainder when the first digit of N is divided by its last digit.

**Examples:**

Input:N = 1234Output:1 First digit = 1 Last digit = 4 Remainder = 1 % 4 = 1Input:N = 5223Output:2 First digit = 5 Last digit = 3 Remainder = 5 % 3 = 2

**Approach:** Find the first digit and the last digit of the number. Find then the remainder when the first digit is divided by the last digit.

Below is the implementation of the above approach:

## C++

`// C++ program to find the remainder ` `// when the First digit of a number ` `// is divided by its Last digit ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the remainder ` `void` `findRemainder(` `int` `n) ` `{ ` ` ` `// Get the last digit ` ` ` `int` `l = n % 10; ` ` ` ` ` `// Get the first digit ` ` ` `while` `(n >= 10) ` ` ` `n /= 10; ` ` ` `int` `f = n; ` ` ` ` ` `// Compute the remainder ` ` ` `int` `remainder = f % l; ` ` ` ` ` `cout << remainder << endl; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` ` ` `int` `n = 5223; ` ` ` ` ` `findRemainder(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the remainder ` `// when the First digit of a number ` `// is divided by its Last digit ` `class` `GFG ` `{ ` ` ` `// Function to find the remainder ` `static` `void` `findRemainder(` `int` `n) ` `{ ` ` ` `// Get the last digit ` ` ` `int` `l = n % ` `10` `; ` ` ` ` ` `// Get the first digit ` ` ` `while` `(n >= ` `10` `) ` ` ` `n /= ` `10` `; ` ` ` `int` `f = n; ` ` ` ` ` `// Compute the remainder ` ` ` `int` `remainder = f % l; ` ` ` ` ` `System.out.println(remainder); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `5223` `; ` ` ` `findRemainder(n); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the remainder ` `# when the First digit of a number ` `# is divided by its Last digit ` ` ` `# Function to find the remainder ` `def` `findRemainder(n): ` ` ` ` ` `# Get the last digit ` ` ` `l ` `=` `n ` `%` `10` ` ` ` ` `# Get the first digit ` ` ` `while` `(n >` `=` `10` `): ` ` ` `n ` `/` `/` `=` `10` ` ` `f ` `=` `n ` ` ` ` ` `# Compute the remainder ` ` ` `remainder ` `=` `f ` `%` `l ` ` ` ` ` `print` `(remainder) ` ` ` `# Driver code ` `n ` `=` `5223` ` ` `findRemainder(n) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the remainder ` `// when the First digit of a number ` `// is divided by its Last digit ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to find the remainder ` `static` `void` `findRemainder(` `int` `n) ` `{ ` ` ` `// Get the last digit ` ` ` `int` `l = n % 10; ` ` ` ` ` `// Get the first digit ` ` ` `while` `(n >= 10) ` ` ` `n /= 10; ` ` ` `int` `f = n; ` ` ` ` ` `// Compute the remainder ` ` ` `int` `remainder = f % l; ` ` ` ` ` `Console.WriteLine(remainder); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `n = 5223; ` ` ` `findRemainder(n); ` `} ` `} ` ` ` `// This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

**Output:**

2

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:

- Program to find remainder when large number is divided by 11
- Program to find remainder when large number is divided by r
- Find the remainder when N is divided by 4 using Bitwise AND operator
- Largest number by which given 3 numbers should be divided such that they leaves same remainder
- Minimum number of operations on a binary string such that it gives 10^A as remainder when divided by 10^B
- Largest number less than or equal to Z that leaves a remainder X when divided by Y
- Count of integers obtained by replacing ? in the given string that give remainder 5 when divided by 13
- Check if the remainder of N-1 factorial when divided by N is N-1 or not
- Smallest subarray whose product leaves remainder K when divided by size of the array
- Last digit of a number raised to last digit of N factorial
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Find all the possible numbers in a range that can be evenly divided by its digits
- First number to leave an odd remainder after repetitive division by 2
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Smallest N digit number with none of its digits as its divisor
- Check if the first and last digit of the smallest number forms a prime
- Find minimum number to be divided to make a number a perfect square
- First N natural can be divided into two sets with given difference and co-prime sums

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.