Given a number to find the first and last digit of a number.
Examples:
Input : 12345
Output : First digit: 1
last digit : 5
Input : 98562
Output : First digit: 9
last digit : 2
To find last digit of a number, we use modulo operator %. When modulo divided by 10 returns its last digit.
Suppose if n = 1234
then last Digit = n % 10 => 4
To find first digit of a number is little expensive than last digit. To find first digit of a number we divide the given number by 10 until number is greater than 10. At the end we are left with the first digit.
Approach 1 (With loop):
C++
#include <bits/stdc++.h>
using namespace std;
int firstDigit( int n)
{
while (n >= 10)
n /= 10;
return n;
}
int lastDigit( int n)
{
return (n % 10);
}
int main()
{
int n = 98562;
cout << firstDigit(n) << " "
<< lastDigit(n) << endl;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
public class GfG{
public static int firstDigit( int n)
{
while (n >= 10 )
n /= 10 ;
return n;
}
public static int lastDigit( int n)
{
return (n % 10 );
}
public static void main(String argc[])
{
int n = 98562 ;
System.out.println(firstDigit(n) + " "
+ lastDigit(n));
}
}
|
Python3
def firstDigit(n) :
while n > = 10 :
n = n / 10 ;
return int (n)
def lastDigit(n) :
return (n % 10 )
n = 98562 ;
print (firstDigit(n), end = " " )
print (lastDigit(n))
|
C#
using System;
public class GfG{
public static int firstDigit( int n)
{
while (n >= 10)
n /= 10;
return n;
}
public static int lastDigit( int n)
{
return (n % 10);
}
public static void Main()
{
int n = 98562;
Console.WriteLine(firstDigit(n) + " "
+ lastDigit(n));
}
}
|
PHP
<?php
function firstDigit( $n )
{
while ( $n >= 10)
$n /= 10;
return (int) $n ;
}
function lastDigit( $n )
{
return ((int) $n % 10);
}
$n = 98562;
echo firstDigit( $n ) . " " .
lastDigit( $n ) . "\n" ;
|
Javascript
<script>
function firstDigit(n)
{
while (n >= 10)
n /= 10;
return Math.floor(n);
}
function lastDigit(n)
{
return Math.floor(n % 10);
}
let n = 98562;
document.write(firstDigit(n) + " "
+ lastDigit(n));
</script>
|
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Approach 2 (Without loop)
C++
#include <bits/stdc++.h>
using namespace std;
int firstDigit( int n)
{
int digits = ( int ) log10 (n);
n = ( int )(n / pow (10, digits));
return n;
}
int lastDigit( int n)
{
return (n % 10);
}
int main()
{
int n = 98562;
cout << firstDigit(n) << " "
<< lastDigit(n) << endl;
return 0;
}
|
Java
import java.math.*;
class GFG {
static int firstDigit( int n)
{
int digits = ( int )(Math.log10(n));
n = ( int )(n / ( int )(Math.pow( 10 , digits)));
return n;
}
static int lastDigit( int n)
{
return (n % 10 );
}
public static void main(String args[])
{
int n = 98562 ;
System.out.println(firstDigit(n) +
" " + lastDigit(n));
}
}
|
Python3
import math
def firstDigit(n) :
digits = ( int )(math.log10(n))
n = ( int )(n / pow ( 10 , digits))
return n;
def lastDigit(n) :
return (n % 10 )
n = 98562 ;
print (firstDigit(n), end = " " )
print (lastDigit(n))
|
C#
using System;
class GFG {
static int firstDigit( int n)
{
int digits = ( int )(Math.Log10(n));
n = ( int )(n / ( int )(Math.Pow(10, digits)));
return n;
}
static int lastDigit( int n)
{
return (n % 10);
}
public static void Main()
{
int n = 98562;
Console.WriteLine(firstDigit(n) +
" " + lastDigit(n));
}
}
|
PHP
<?php
function firstDigit( $n )
{
$digits = (int)log10( $n );
$n = (int)( $n / pow(10, $digits ));
return $n ;
}
function lastDigit( $n )
{
return ( $n % 10);
}
$n = 98562;
echo firstDigit( $n ) , " " ,
lastDigit( $n ), "\n" ;
?>
|
Javascript
<script>
function firstDigit(n)
{
let digits = Math.floor(Math.log(n)/Math.log(10))
n = Math.floor(n / Math.pow(10, digits))
return n;
}
function lastDigit(n){
return (n % 10)
}
let n = 98562;
document.write(firstDigit(n), " " )
document.write(lastDigit(n), "</br>" )
</script>
|
Time Complexity: O(log(log10(n))
Auxiliary Space: O(1)
Important note: log10() is a mathematical function present in math.h header file. It returns log base 10 value of the passed parameter to log10() function.
Approach 3: Using string operation to_string
First converting number to string and then first char of string and last character of string – ‘0’ is our ans.
C++
#include <iostream>
using namespace std;
int main() {
int n = 34356;
string s = to_string(n);
int first_digit = s.front() - '0' ;
int last_digit = s.back() - '0' ;
cout<< "First digit of " <<n<< " is " <<first_digit<<endl;
cout<< "Last digit of " <<n<< " is " <<last_digit<<endl;
}
|
Java
import java.util.*;
public class Main {
public static void main(String[] args) {
int n = 34356 ;
String s = Integer.toString(n);
int first_digit = s.charAt( 0 ) - '0' ;
int last_digit = s.charAt(s.length() - 1 ) - '0' ;
System.out.println( "First digit of " + n + " is " + first_digit);
System.out.println( "Last digit of " + n + " is " + last_digit);
}
}
|
Python3
n = 34356
s = str (n)
first_digit = int (s[ 0 ])
last_digit = int (s[ - 1 ])
print ( "First digit of" , n, "is" , first_digit)
print ( "Last digit of" , n, "is" , last_digit)
|
C#
using System;
public class GFG{
static public void Main (){
int n = 34356;
string s = n.ToString();
int first_digit = s[0] - '0' ;
int last_digit = s[s.Length - 1] - '0' ;
Console.WriteLine( "First digit of {0} is {1}" , n, first_digit);
Console.WriteLine( "Last digit of {0} is {1}" , n, last_digit);
}
}
|
Javascript
let n = 34356;
let s = n.toString();
let first_digit = parseInt(s.charAt(0));
let last_digit = parseInt(s.charAt(s.length-1));
console.log( "First digit of " + n + " is " + first_digit);
console.log( "Last digit of " + n + " is " + last_digit);
|
OutputFirst digit of 34356 is 3
Last digit of 34356 is 6
Time Complexity: O(n)
Auxiliary Space: O(n)