Write a function Third_last_digit in C++ and call the function that print third last digit of the given number. The third last digit is being referred to the digit in the hundreds place in the given number.
For example, if the given number is 3197, the third last digit is 1.
Note 1 - The third last digit should be printed as a positive number. i.e. if the given number is -197, the third last digit is 1.
Note 2 - If the given number is a single-digit or double-digit number, then the third last digit does not exist. In such cases, the program should print -1. i.e. if the given number is 5, the third last digit should be print as -1
#include <iostream>
using namespace std;
Â
int Third_last_digit(int);
int main()Â {
// declare and initialize variables
  int x, n;
Â
  cout << "Enter number: ";
  cin >> n;
Â
  x = Third_last_digit(n);
  cout << "The third last digit of "<< n << " is " << x <<endl;
Â
  cout << "Enter number: ";
  cin >> n;
Â
  x = Third_last_digit(n);
  cout << "The third last digit of "<< n << " is " << x <<endl<<endl;           Â
Â
  return 0;
}
int Third_last_digit(int x )Â {
 // Handle negative number
  if (x < 0)
      x = -x;
Â
  if(x < 100) // If the given number
       x =-1;   // is a single-digit or double-digit number
                    // the program should print -1
  else
      x = x/100%10;
Â
  return x;
Â
}
Comments
Leave a comment