Write a recursive method that enumerates and prints all n-digit even numbers in
ID: 3710129 • Letter: W
Question
Write a recursive method that enumerates and prints all n-digit even numbers in which each digit of the number, from its most-significant to least-significant digits, is greater than the previous one (that is, for instance, the number 124 is ok, but the number 142 is not, since 2 is less than 4). Make sure your program prints each number just once. For example, if n=3 it should print 124 126 128 134 136 138 146 148 156 and so on up to 999. Hint: If you had a method that checked whether a number meets the stated criteria or not, you could use it to filter the candidate values you generated recursively, printing only those that meet the criteria ~ideally the criteria checking method would also be recursive.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
void even_number(int num, int ending_number, int no_of_digits)
{
/*
* If the number is greater than or equal to ending number return.
* Used as a condition to terminate recursive functionality
*/
if (num >= ending_number)
return;
/* Check if the number is even or not */
if ((num % 2) == 0) {
int n = num;
/* Initialize the two digits to -1 which are used to compare */
int cur_digit = -1;
int prev_digit = -1;
do {
/* If first time checking */
if ((cur_digit == -1) && (prev_digit == -1)) {
/* Get the Most significant digit */
prev_digit = n % 10;
n = n / 10;
/* Get the digit which is before Most significant digit */
cur_digit = n % 10;
n = n / 10;
} else {
/* Get the remaining digits to compare */
prev_digit = cur_digit;
cur_digit = n % 10;
n = n / 10;
}
/*
* Compare each digit with its previous one.
* If the previous one is less than the current
* one stop processing that number
* Eg: for 120, In first iteration prev_digit = 0 and
* cur_digit = 2;
*/
if (prev_digit <= cur_digit)
break;
} while (n);
/* Print the number that met all the conditions */
if (!n && (prev_digit > cur_digit))
printf("%d ", num);
}
/* Call the function recursively */
even_number(num + 1, ending_number, no_of_digits);
}
int main(int argc, char **argv)
{
int no_of_digits = 0;
int starting_num = 1;
int ending_num = 1;
int i = 0;
if (argc != 2) {
printf("Usage: ./a.out no_of_digits ");
printf("Eg: ./a.out 3 ");
return -1;
}
/* convert command line argument from string to integer */
no_of_digits = atoi(argv[1]);
/* Get starting number */
/* Eg: if no_of_digits is 3, starting number should be 10*10*10 */
for (i = 0; i < no_of_digits - 1; i++)
starting_num *= 10;
printf("Starting Number : %d ", starting_num);
/* Find ending_number */
/* Eg: If strating number is 100 ending number should be ((100 * 10) - 1 = 999) */
ending_num = (starting_num * 10) - 1;
/* Call even_number recursively to calculate and print the numbers */
even_number(starting_num, ending_num, no_of_digits);
printf(" ");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.