Find first and last digits of a number | Program to find first and last digits of a number ; Find the first digit ; Remove last digit from number till only one digit is left ; return the first digit ; Find the last digit ; return the last digit ; Driver program
#include <bits/stdc++.h> NEW_LINE 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 ; }