|
| 1 | +// Copyright 2022 finchren |
| 2 | +/* |
| 3 | +Write a C program to find Strong Numbers within a range of numbers |
| 4 | +Test Data : |
| 5 | +Input starting range of number : 1 |
| 6 | +Input ending range of number: 200 |
| 7 | +Expected Output : |
| 8 | +The Strong numbers are : |
| 9 | +1 2 145 |
| 10 | +
|
| 11 | +Rework regular Sstrong number solution to work in a range: |
| 12 | + + 1. Correct the input |
| 13 | + + 2. Find numbers inside a range |
| 14 | + + 3. Test: |
| 15 | + Input: 1 and 1000000. Output: |
| 16 | + 1 2 145 40585 |
| 17 | + + 4. Cpplint |
| 18 | + + 5. Add and push |
| 19 | +*/ |
| 20 | + |
| 21 | +#include <stdio.h> |
| 22 | + |
| 23 | +int input_start(); |
| 24 | +int input_end(); |
| 25 | +void print_invalid_input(); |
| 26 | +int find_number_of_digits(int number); |
| 27 | +int find_factorial(int number); |
| 28 | +int power(int base, int exponent); |
| 29 | +int is_strong(int number); |
| 30 | +void print_is_strong(int start, int end); |
| 31 | + |
| 32 | +int main() { |
| 33 | + int start = input_start(); |
| 34 | + if (start >= 0) { |
| 35 | + int end = input_end(); |
| 36 | + if (end >= 0) { |
| 37 | + print_is_strong(start, end); |
| 38 | + } else { |
| 39 | + print_invalid_input(); |
| 40 | + } |
| 41 | + } else { |
| 42 | + print_invalid_input(); |
| 43 | + } |
| 44 | + return 0; |
| 45 | +} |
| 46 | + |
| 47 | +void print_is_strong(int start, int end) { |
| 48 | + printf("The Strong numbers are:\n"); |
| 49 | + for (int i = start; i <= end; i++) { |
| 50 | + if (is_strong(i)) { |
| 51 | + printf("%d ", i); |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +int is_strong(int number) { |
| 57 | + int is_strong = 0, result = 0, temp_number = number, |
| 58 | + one_digit, number_of_digits = find_number_of_digits(number); |
| 59 | + for (int i = number_of_digits; i > 0; --i) { |
| 60 | + one_digit = temp_number / (1 * power(10, i - 1)); |
| 61 | + temp_number -= one_digit * (1 * power(10, i - 1)); |
| 62 | + result += find_factorial(one_digit); |
| 63 | + } |
| 64 | + if (number == result) { |
| 65 | + is_strong = 1; |
| 66 | + } |
| 67 | + return is_strong; |
| 68 | +} |
| 69 | + |
| 70 | +int find_factorial(int number) { |
| 71 | + int result = 1; |
| 72 | + for (int i = 1; i <= number; i++) { |
| 73 | + result *= i; |
| 74 | + } |
| 75 | + return result; |
| 76 | +} |
| 77 | + |
| 78 | +int find_number_of_digits(int number) { |
| 79 | + int result = 0; |
| 80 | + for (int i = number; i > 0; i /= 10) { |
| 81 | + ++result; |
| 82 | + } |
| 83 | + return result; |
| 84 | +} |
| 85 | + |
| 86 | +int power(int base, int exponent) { |
| 87 | + int result = base; |
| 88 | + if (exponent != 0) { |
| 89 | + for (int i = 2; i <= exponent; i++) { |
| 90 | + result *= base; |
| 91 | + } |
| 92 | + } else { |
| 93 | + result = 1; |
| 94 | + } |
| 95 | + return result; |
| 96 | +} |
| 97 | + |
| 98 | +void print_invalid_input() { |
| 99 | + printf("n/a"); |
| 100 | +} |
| 101 | + |
| 102 | +int input_end() { |
| 103 | + int end; |
| 104 | + char endline; |
| 105 | + printf("Input starting range of number:\n"); |
| 106 | + if (!scanf("%d%c", &end, &endline) || endline != '\n') { |
| 107 | + end = -1; |
| 108 | + } |
| 109 | + return end; |
| 110 | +} |
| 111 | + |
| 112 | +int input_start() { |
| 113 | + int start; |
| 114 | + char endline; |
| 115 | + printf("Input ending range of number:\n"); |
| 116 | + if (!scanf("%d%c", &start, &endline) || endline != '\n') { |
| 117 | + start = -1; |
| 118 | + } |
| 119 | + return start; |
| 120 | +} |
0 commit comments