# Find first and last digits of a number

Given a number and to find 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
```

## Recommended: Please solve it on PRACTICE first, before moving on to the solution.

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

 `// Program to find first and last ` `// digits of a number ` `#include ` `using` `namespace` `std; ` ` `  `// Find the first digit ` `int` `firstDigit(``int` `n) ` `{ ` `    ``// Remove last digit from number ` `    ``// till only one digit is left ` `    ``while` `(n >= 10)  ` `        ``n /= 10; ` `     `  `    ``// return the first digit ` `    ``return` `n; ` `} ` ` `  `// Find the last digit ` `int` `lastDigit(``int` `n) ` `{ ` `    ``// return the last digit ` `    ``return` `(n % 10); ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `n = 98562; ` `    ``cout << firstDigit(n) << ``" "`  `        ``<< lastDigit(n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java Program to find first and last ` `// digits of a number ` `import` `java.util.*; ` `import` `java.lang.*; ` ` `  `public` `class` `GfG{ ` `     `  `    ``// Find the first digit ` `    ``public` `static` `int` `firstDigit(``int` `n) ` `    ``{ ` `        ``// Remove last digit from number ` `        ``// till only one digit is left ` `        ``while` `(n >= ``10``)  ` `            ``n /= ``10``; ` `     `  `        ``// return the first digit ` `        ``return` `n; ` `    ``} ` ` `  `    ``// Find the last digit ` `    ``public` `static` `int` `lastDigit(``int` `n) ` `    ``{ ` `        ``// return the last digit ` `        ``return` `(n % ``10``); ` `    ``} ` `     `  `    ``// driver function ` `    ``public` `static` `void` `main(String argc[]) ` `    ``{ ` `        ``int` `n = ``98562``; ` `        ``System.out.println(firstDigit(n) + ``" "` `        ``+ lastDigit(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Sagar Shukla `

## Python3

 `# Python3 program to find first and  ` `# last digits of a number ` ` `  `# Find the first digit ` `def` `firstDigit(n) : ` ` `  `    ``# Remove last digit from number ` `    ``# till only one digit is left ` `    ``while` `n >``=` `10``:  ` `        ``n ``=` `n ``/` `10``; ` `     `  `    ``# return the first digit ` `    ``return` `int``(n) ` ` `  `# Find the last digit ` `def` `lastDigit(n) : ` ` `  `    ``# return the last digit ` `    ``return` `(n ``%` `10``) ` ` `  `# Driver Code ` `n ``=` `98562``; ` `print``(firstDigit(n), end ``=` `" "``)  ` `print``(lastDigit(n)) ` ` `  `# This code is contributed by rishabh_jain `

## C#

 `// C# Program to find first and last ` `// digits of a number ` `using` `System; ` ` `  `public` `class` `GfG{ ` `     `  `    ``// Find the first digit ` `    ``public` `static` `int` `firstDigit(``int` `n) ` `    ``{ ` `        ``// Remove last digit from number ` `        ``// till only one digit is left ` `        ``while` `(n >= 10)  ` `            ``n /= 10; ` `     `  `        ``// return the first digit ` `        ``return` `n; ` `    ``} ` ` `  `    ``// Find the last digit ` `    ``public` `static` `int` `lastDigit(``int` `n) ` `    ``{ ` `        ``// return the last digit ` `        ``return` `(n % 10); ` `    ``} ` `     `  `    ``// driver function ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 98562; ` `        ``Console.WriteLine(firstDigit(n) + ``" "` `        ``+ lastDigit(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by vt_m `

## PHP

 `= 10)  ` `        ``\$n` `/= 10; ` `     `  `    ``// return the first digit ` `    ``return` `(int)``\$n``; ` `} ` ` `  `// Find the last digit ` `function` `lastDigit(``\$n``) ` `{ ` `    ``// return the last digit ` `    ``return` `((int)``\$n` `% 10); ` `} ` ` `  `// Driver Code ` `\$n` `= 98562; ` `echo` `firstDigit(``\$n``) . ``" "` `. ` `     ``lastDigit(``\$n``) . ``"\n"``; ` ` `  `// This code is contributed  ` `// by Akanksha Rai(Abby_akku) `

Output:

```9 2
```

Approach 2 (Without loop)

## C++

 `// Program to find first and last  ` `// digits of a number ` `#include ` `using` `namespace` `std; ` ` `  `// Find the first digit ` `int` `firstDigit(``int` `n) ` `{ ` `    ``// Find total number of digits - 1 ` `    ``int` `digits = (``int``)``log10``(n); ` ` `  `    ``// Find first digit ` `    ``n = (``int``)(n / ``pow``(10, digits)); ` ` `  `    ``// Return first digit ` `    ``return` `n; ` `} ` ` `  `// Find the last digit ` `int` `lastDigit(``int` `n) ` `{ ` `    ``// return the last digit ` `    ``return` `(n % 10); ` `} ` ` `  `// Driver program ` `int` `main() ` `{ ` `    ``int` `n = 98562; ` `    ``cout << firstDigit(n) << ``" "`  `         ``<< lastDigit(n) << endl; ` `    ``return` `0; ` `} `

## Java

 `// Java program to find first and  ` `// last  digits of a number ` `import` `java.math.*; ` ` `  `class` `GFG { ` `     `  `    ``// Find the first digit ` `    ``static` `int` `firstDigit(``int` `n) ` `    ``{ ` `        ``// Find total number of digits - 1 ` `        ``int` `digits = (``int``)(Math.log10(n)); ` `     `  `        ``// Find first digit ` `        ``n = (``int``)(n / (``int``)(Math.pow(``10``, digits))); ` `     `  `        ``// Return first digit ` `        ``return` `n; ` `    ``} ` `     `  `    ``// Find the last digit ` `    ``static` `int` `lastDigit(``int` `n) ` `    ``{ ` `        ``// return the last digit ` `        ``return` `(n % ``10``); ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``98562``; ` `        ``System.out.println(firstDigit(n) + ` `                           ``" "` `+ lastDigit(n)); ` `    ``} ` `} ` ` `  ` `  `// This code is contributed by Nikita Tiwari. `

## Python3

 `# Python3 program to find first   ` `# and last digits of a number ` `import` `math ` ` `  `# Find the first digit ` `def` `firstDigit(n) : ` `     `  `    ``# Find total number of digits - 1 ` `    ``digits ``=` `(``int``)(math.log10(n)) ` ` `  `    ``# Find first digit ` `    ``n ``=` `(``int``)(n ``/` `pow``(``10``, digits)) ` ` `  `    ``# Return first digit ` `    ``return` `n; ` ` `  `# Find the last digit ` `def` `lastDigit(n) : ` `     `  `    ``# return the last digit ` `    ``return` `(n ``%` `10``) ` ` `  `# Driver Code ` `n ``=` `98562``; ` `print``(firstDigit(n), end ``=` `" "``)  ` `print``(lastDigit(n)) ` ` `  `# This code is contributed by rishabh_jain `

## C#

 `// C# program to find first and  ` `// last digits of a number ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Find the first digit ` `    ``static` `int` `firstDigit(``int` `n) ` `    ``{ ` `        ``// Find total number of digits - 1 ` `        ``int` `digits = (``int``)(Math.Log10(n)); ` `     `  `        ``// Find first digit ` `        ``n = (``int``)(n / (``int``)(Math.Pow(10, digits))); ` `     `  `        ``// Return first digit ` `        ``return` `n; ` `    ``} ` `     `  `    ``// Find the last digit ` `    ``static` `int` `lastDigit(``int` `n) ` `    ``{ ` `        ``// return the last digit ` `        ``return` `(n % 10); ` `    ``} ` `     `  `    ``// Driver program ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int` `n = 98562; ` `        ``Console.WriteLine(firstDigit(n) + ` `                        ``" "` `+ lastDigit(n)); ` `    ``} ` `} ` ` `  ` `  `// This code is contributed by vt_m. `

## PHP

 ` `

Output:

```9 2
```

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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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 : jit_t, Akanksha_Rai

Article Tags :

8

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.