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

Question #177521

Write a function second_last_digit in C++ and call function 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.


1
Expert's answer
2021-04-01T06:05:41-0400
#include <iostream>
using namespace std;
int second_last_digit(int x){
    x = abs(x);
    if(x >= 10){
        return (x/10)%10;
    }
    else return -1;
}
int main(){
    int x;
    cout<<"Input the integer to find the second last digit.\n";
    cin>>x;
    if(abs(x) > 10)
        cout<<"The second last digit of "<<x<<" is ";
    cout<<second_last_digit(x);
    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