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

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

2


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : mohit kumar 29, Code_Mech