Find the remainder when First digit of a number is divided by its Last digit - GeeksforGeeks

21 Apr, 2021
Given a number N, find the remainder when the first digit of N is divided by its last digit.Examples: 
Input: N = 1234
Output: 1
First digit = 1
Last digit = 4
Remainder = 1 % 4 = 1

Input: N = 5223
Output: 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++
Java
Python3
C#
Javascript
// 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 remaindervoid 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 codeint main(){     int n = 5223;     findRemainder(n);     return 0;}
// Java program to find the remainder// when the First digit of a number// is divided by its Last digitclass GFG{     // Function to find the remainderstatic 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 codepublic static void main(String[] args){    int n = 5223;    findRemainder(n);}} // This code is contributed by Code_Mech
# Python3 program to find the remainder# when the First digit of a number# is divided by its Last digit # Function to find the remainderdef 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 coden = 5223 findRemainder(n) # This code is contributed by Mohit Kumar
// C# program to find the remainder// when the First digit of a number// is divided by its Last digitusing System; class GFG{     // Function to find the remainderstatic 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 codepublic static void Main(){    int n = 5223;    findRemainder(n);}} // This code is contributed by Code_Mech
<script>  // Javascript  program to find the remainder// when the First digit of a number// is divided by its Last digit // Function to find the remainderfunction findRemainder( n){    // Get the last digit    let  l = n % 10;     // Get the first digit    while (n >= 10)        n /= 10;    let f = n;    // Compute the remainder    let remainder = f % l;     document.write(Math.floor(remainder));} // Driver code    let n = 5223;    findRemainder(n);  // This code is contributed by mohan pavan </script>
2
 
Time Complexity: O(L ) where L is length of number in decimal representation
Auxiliary Space: O(1)
mohit kumar 29
Code_Mech
ujjwalgoel1103
pulamolusaimohan
number-digits
school-programming
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Prime Numbers
Program to find GCD or HCF of two numbers
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Program to find sum of elements in a given array
The Knight's tour problem | Backtracking-1
Program for factorial of a number
Operators in C / C++