Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following functions: a. int first_digit(int n), returning the first di

ID: 3552030 • Letter: W

Question

Write the following functions:

a. int first_digit(int n), returning the first digit of the argument.

b. int last_digit(int n), returning the last digit of the argument.

c. int digits(int n), returning the number of digits of the argument.

For example, first_ digit(1729) returns 1, last_ digit(1729) returns 9, and digits(1729) returns 4.

Provide a program that tests all three of your functions via a loop construct for acquiring testing data.

Example run (with user input indicated with bold italics):

Enter number or Q to quit: 1729

First digit: 1 Last digit: 9 Number of digits: 4

Enter number or Q to quit: 1234567

First digit: 1 Last digit: 7 Number of digits: 7

Enter number or Q to quit: Q

Explanation / Answer

#include<iostream>


using namespace std;




int first_digit(int n){

int res;

while(n > 0){

res = n;

n = n/10;

}

return res;

}


int last_digit(int n){

return n%10;

}


int digits(int n){

int count = 0;


while(n > 0){

n = n/10;

count++;

}

}



int main(){

int num;


while(1){

cout << "Enter number or Q to quit: ";

cin >> num;


if(num == 'Q'){

break;

}


else{

cout << "First digit: " << first_digit(num) << " Last digit: " << last_digit(num) << " Number of digits: " << digits(num) << endl;

}

}


return 0;


}