Absolute difference between the first X and last X Digits of N - GeeksforGeeks

01 Oct, 2021
Given two integers N and X. The task is to print the absolute difference between the first X and last X digits in N. Considering the number of digits is atleast 2*x.
Examples: 
Input: N = 21546, X = 2
Output: 25
The first two digit in 21546 is 21.
The last two digit in 21546 is 46.
The absolute difference of 21 and 46 is 25.

Input: N = 351684617, X = 3
Output: 266
Simple Approach:  
Store the last x digits of the number in last.
Find the number of digits in the number.
Remove all the digits except the first x.
Store the first x integers of the number in first.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std; // Function to find the// number of digits in the integerlong long digitsCount(long long n){    int len = 0;    while (n > 0) {        len++;        n /= 10;    }    return len;} // Function to find the absolute differencelong long absoluteFirstLast(long long n, int x){    // Store the last x digits in last    int i = 0, mod = 1;    while (i < x) {        mod *= 10;        i++;    }    int last = n % mod;     // Count the no. of digits in N    long long len = digitsCount(n);     // Remove the digits except the first x    while (len != x) {        n /= 10;        len--;    }     // Store the first x digits in first    int first = n;     // Return the absolute difference between    // the first and last    return abs(first - last);} // Driver codeint main(){    long long n = 21546, x = 2;    cout << absoluteFirstLast(n, x);     return 0;}
// Java implementation of the above approachimport java.util.*; class GFG{ // Function to find the// number of digits in the integerstatic int digitsCount(int n){    int len = 0;    while (n > 0)    {        len++;        n /= 10;    }    return len;} // Function to find the absolute differencestatic int absoluteFirstLast(int n, int x){    // Store the last x digits in last    int i = 0, mod = 1;    while (i < x)    {        mod *= 10;        i++;    }    int last = n % mod;     // Count the no. of digits in N    int len = digitsCount(n);     // Remove the digits except the first x    while (len != x)    {        n /= 10;        len--;    }     // Store the first x digits in first    int first = n;     // Return the absolute difference between    // the first and last    return Math.abs(first - last);} // Driver codepublic static void main(String args[]){    int n = 21546, x = 2;    System.out.println(absoluteFirstLast(n, x));}} // This code is contributed by// Surendra_Gangwar
# Python3 implementation of the above approach # Function to find the# number of digits in the integerdef digitsCount(n) :    length = 0;    while (n > 0) :        length += 1;        n //= 10;             return length;  # Function to find the absolute differencedef absoluteFirstLast(n, x) :         # Store the last x digits in last    i = 0 ;    mod = 1;    while (i < x) :        mod *= 10;        i += 1;         last = n % mod;         # Count the no. of digits in N    length = digitsCount(n);         # Remove the digits except the first x    while (length != x) :        n //= 10;        length -= 1;         # Store the first x digits in first    first = n;         # Return the absolute difference between    # the first and last    return abs(first - last); # Driver codeif __name__ == "__main__" :         n = 21546 ;    x = 2;    print(absoluteFirstLast(n, x));     # This code is contributed by Ryuga
// C# implementation of the approachusing System; class GFG{ // Function to find the// number of digits in the integerstatic int digitsCount(int n){    int len = 0;    while (n > 0)    {        len++;        n /= 10;    }    return len;} // Function to find the absolute differencestatic int absoluteFirstLast(int n, int x){    // Store the last x digits in last    int i = 0, mod = 1;    while (i < x)    {        mod *= 10;        i++;    }    int last = n % mod;     // Count the no. of digits in N    int len = digitsCount(n);     // Remove the digits except the first x    while (len != x)    {        n /= 10;        len--;    }     // Store the first x digits in first    int first = n;     // Return the absolute difference between    // the first and last    return Math.Abs(first - last);} // Driver codepublic static void Main(String []args){    int n = 21546, x = 2;    Console.Write(absoluteFirstLast(n, x));}} // This code has been contributed by 29AjayKumar
<?php// PHP implementation of the above approach // Function to find the number of// digits in the integerfunction digitsCount($n){    $len = 0;    while ($n > 0)    {        $len++;        $n = (int)($n / 10);    }    return $len;} // Function to find the absolute differencefunction absoluteFirstLast($n, $x){    // Store the last x digits in last    $i = 0;    $mod = 1;    while ($i < $x)    {        $mod *= 10;        $i++;    }    $last = $n % $mod;     // Count the no. of digits in N    $len = digitsCount($n);     // Remove the digits except the first x    while ($len != $x)    {        $n = (int)($n / 10);        $len--;    }     // Store the first x digits in first    $first = $n;     // Return the absolute difference    // between the first and last    return abs($first - $last);} // Driver code$n = 21546;$x = 2;echo absoluteFirstLast($n, $x); // This code is contributed by mits?>
<script> // Javascript implementation of the above approach // Function to find the// number of digits in the integerfunction digitsCount(n){    let len = 0;    while (n > 0)    {        len++;        n = Math.floor(n / 10);    }    return len;} // Function to find the absolute differencefunction absoluteFirstLast(n, x){    // Store the last x digits in last    let i = 0, mod = 1;    while (i < x)    {        mod *= 10;        i++;    }    let last = n % mod;     // Count the no. of digits in N    let len = digitsCount(n);     // Remove the digits except the first x    while (len != x)    {        n = Math.floor(n / 10);        len--;    }     // Store the first x digits in first    let first = n;     // Return the absolute difference between    // the first and last    return Math.abs(first - last);} // driver program         let n = 21546, x = 2;    document.write(absoluteFirstLast(n, x));   </script>
25
 
ankthon
SURENDRA_GANGWAR
29AjayKumar
Mithun Kumar
code_hunt
simranarora5sos
math
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Program to find GCD or HCF of two numbers
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Program for Decimal to Binary Conversion
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
C++ Classes and Objects