📌  相关文章
📜  c++代码示例中数字的第一位和最后一位数字

📅  最后修改于: 2022-03-11 14:44:58.851000             🧑  作者: Mango

代码示例1
// 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;
}