给定一个数字并查找数字的第一位和最后一位。
例子:
Input : 12345
Output : First digit: 1
last digit : 5
Input : 98562
Output : First digit: 9
last digit : 2
要查找数字的最后一位,我们使用模运算符% 。当取10的模数时,返回其最后一位。
假设n = 1234
然后最后一位= n%10 => 4
查找数字的第一个数字比最后一个数字便宜。为了找到数字的第一位数字,我们将给定的数字除以10,直到数字大于10。最后,我们剩下第一位数字。
方法1(带循环):
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)
Javascript
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
输出:
9 2
方法2(无循环)
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
输出:
9 2
重要说明: log10()是math.h头文件中存在的数学函数。它将传递的参数的以10为底的日志值返回到log10()函数。