![]() |
Given a Number, the task is to find the first, and last digits of a number using PHP. Examples: Input: num = 145677 There are three methods to get the first and last digit of number, these are:
Table of Content Using LoopIn this section, we use while loop to get the first and last digit of number. Remainder operator is used to get the last digit of number, and for first digit, use while loop. PHP
Output
First Digit: 1, Last Digit: 5 Using LogarithmHere, we use Logarithm to get the first digit of number. To get the first digit, divide the number by 10 power length of given number i.e. $number / pow(10, $len). Example: PHP
Output
First Digit: 1, Last Digit: 5 Using str_split() FunctionIn this section, first, we convert the number of string and then split into an array, then get the first and last element of array. PHP
Output
First Digit: 1, Last Digit: 5 |
Reffered: https://www.geeksforgeeks.org
Geeks Premier League |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |