Answer to Question #174159 in C++ for V.Karthickraja

Question #174159

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-03-22T09:49:46-0400
#include <iostream>
using std::cout;
using std::endl;
int second_last_digit(int n) {
	if (n < 0)
		n = -n;
	n = n/10;
	if (n == 0)
		return -1;
	else
		return n - 10 * (n / 10);
}
int main()
{
	//test function 
	cout<<second_last_digit(21)<<endl;
	cout << second_last_digit(-548) << endl;
	cout << second_last_digit(5)<<endl;
	cout << second_last_digit(204) << endl;
	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