# Quiz 10: More Linked Lists

Computing

Program plan: • Include the required header file • Declare the function "no_Digits()" for counting the digits o The "if" condition checks whether the integer "n" is less than 10. • If the above "if" condition is true then return 1. • If the condition is not true then it goes to the else part. o The "else" part returns "1+ no_Digits (n / 10)". • Define the main function "main()" o Call the function "no_Digits" by passing the parameter and assign the function to an unsigned variable "a". o Display the variable "a". Program: / ********************************************************** * Program to demonstrate recursive digit-counting function* ********************************************************** / / / include required header files #include using namespace std; / / function definition int no_Digits(unsigned n) { / / it checks whether 'n' is less than 10 if (n 10) / / if the above condition is true / / then return 1 return 1; else / / if the 'if' condition fails it returns / / 1+f(n / 10) return 1 + no_Digits (n / 10); } / / main function int main() { / / function call unsigned a = no_Digits (123456); / / displaying the result cout a / / pause the screen system("pause"); / / return 0 return 0; } Sample Output: The output displays the "number of digits". 6

Problem plan: • Include the required header files. • Define the function "no_Digits()" for counting the digits. o Declare a variable "cnt" and initialize it to 1. o The "while" loop runs till the integer "n" is less than 10. • Increment the "cnt" variable. • Update the variable "n" by dividing "n" by 10 o Return "cnt" variable. • Declare the main function "main()" o Call the function "no_Digits()" by passing the parameter and assign the function to an unsigned variable "a". o Display the variable "a". Program: / ********************************************************** * Program to count the number of digits in the given value* *using non recursion * ********************************************************** / / / include required header files #include using namespace std; / / function definition int no_Digits(unsigned n) { / / declare a variable 'a' and initialize it to 1 int cnt = 1; / / this loop runs till 'n' is less than 10 while (n = 10) { / / increment the variable 'a' cnt++; / / update the variable 'n' by dividing 'n' by 10 n = n / 10; } / / return the variable 'a' return cnt; } / / main function int main() { / / function call unsigned a = no_Digits(1234567); / / displaying the result cout a / / pause the screen system("pause"); / / return 0 return 0; } Sample output: 7

Program plan: • Include the required header file • Define the function "printReverse()" for print the digit reverse o The "if" statement checks whether the integer "n" is less than 10. • If the condition is true then display "n" o If the condition is not true, then it goes to the "else" part. • The "else" part displays "n%10" value. • Call the recursive function by passing "n / 10" as the parameter. • Define the main function "main()". o Call the function "printReverse()" by passing the parameter. Program: / ********************************************************** * Program to display the number in the reverse order with * * recursion * ********************************************************** / / / include required header files #include using namespace std; / / function definition void printReverse(unsigned n) { / / this checks whether the integer 'n' is less than 10 if (n 10) / / display the integer 'n' cout n endl; else { / / display n%10 value cout n % 10; / * call the recursive function by passing n / 10 as the parameter * / printReverse (n / 10); } } / / main function int main() { / / function call printReverse (1234567); / / pause the screen system("pause"); / / return 0 return 0; } Sample output: 7654321