Answers for "print last digit in a number in c++"

C++
0

c++ string find last number

//Example
string str = "sdfsd34";

//Find the first first number and then all the number after
str.substr(str.find_first_of("0123456789"))

//Find the last number
size_t last_index = str.find_last_not_of("0123456789");
string result = str.substr(last_index + 1);
Posted by: Guest on November-22-2021
0

first and last digit of a number in c++

// Program to find first and last
// digits of a number
#include <bits/stdc++.h>
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;
}
Posted by: Guest on December-28-2021

Code answers related to "print last digit in a number in c++"

Browse Popular Code Answers by Language