# find first and last digit of a number without using loop

0
488 Welcome back to shortlearner.com, in this post we will see how to write a program to find first and last digit of a number without using any type of loop.

so in the below program we are declaring a php variable \$n which will takes the number.
here we are defining 2 php functions which will returns the first and last digit of our \$n number.

the first function is firstNumber which will accept \$n as a parameter than we use log10() function.

now we will take an short overview of log10() function. basically log10() is a mathematical function which is defined in math.h header file and it returns log base 10 value of the passed paramater.

than we use pow() function. so basically pow() is a predefined function which will return the first number to the power of second number. in other words we can say that it compute power of a certain number. this function takes 2 parameter which are the base and exponent and returns the desired answer.

after using pow() function we will get first digit of our \$n number. now we will declare our second function lastNumer which will help us to find out the last digit of \$n number.

``````<?php
function firstNumber(\$n)
{
// Find total number of digits - 1
\$digits = (int)log10(\$n);

// Find first digit
\$n = (int)(\$n / pow(10, \$digits));

// Return first digit
return \$n;
}

// Find the last digit
function lastNumer(\$n)
{
// return the last digit
return (\$n % 10);
}

// Define numer
\$n = 58727;
echo firstNumber(\$n) , " ",
lastNumer(\$n), "\n";

// our out put is 5 and 7.
?> ``````

If these article helps you please share it with your developers buddy.
and if you are a web designer and looking for amazing snippets thank click on the below button.

Keep Learning, Keep Coding