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