Answer to Question #177519 in C++ for P. Gokul

Question #177519

Write a program that print the second last digit of the given number. The second last digit is being referred to the digit in the tens place in the given number.

For example, if the given number is 197, the second last digit is 9.

Note 1 - The second last digit should be returned as a positive number. i.e. if the given number is -197, the second last digit is 9.

Note 2 - If the given number is a single-digit number, then the second last digit does not exist. In such cases, the program should print -1. i.e. if the given number is 5, the second last digit should be print as -


1
Expert's answer
2021-04-01T08:43:11-0400
#include <iostream>
using namespace std;


int printNum(int n){
    if (abs(n)<10)
        return -1;
    else
    {
        string s=to_string(n); 
        return s[s.size()-2]-'0';
    }
}
int main()
{
    cout<< printNum(-3);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS