|
| 1 | +// Copyright 2022 finchren |
| 2 | +/* |
| 3 | +Write a C program to check whether a number is a Strong Number or not |
| 4 | +Test Data : |
| 5 | +Input a number to check whether it is a Strong number: 15 |
| 6 | +Expected Output : |
| 7 | +15 is not a Strong number. |
| 8 | +
|
| 9 | ++ 1. Take input |
| 10 | ++ 2. Invalid input |
| 11 | ++ 3.0 Figure out what is a Strong number: |
| 12 | + 145 = 1! + 4! + 5! |
| 13 | ++ 3.1 Write a factorial function |
| 14 | ++ 3.2 Number of digits function |
| 15 | ++ 3.3 Write power function |
| 16 | ++ 3.3 Write function to check if Strong or not |
| 17 | ++ 4. Output |
| 18 | ++ 5. Test |
| 19 | ++ 6. Cpplint test |
| 20 | ++ 7. Add and push |
| 21 | +*/ |
| 22 | + |
| 23 | +#include <stdio.h> |
| 24 | + |
| 25 | +int input_number(); |
| 26 | +void print_invalid_input(); |
| 27 | +int find_number_of_digits(int number); |
| 28 | +int find_factorial(int number); |
| 29 | +int power(int base, int exponent); |
| 30 | +int is_strong(int number); |
| 31 | +void print_is_armstrong(int number, int is_strong); |
| 32 | +void test(int number, int expected, int test_number); |
| 33 | + |
| 34 | +int main() { |
| 35 | + // Regular run of the program |
| 36 | + int number = input_number(); |
| 37 | + if (number >= 0) { |
| 38 | + int is_strong_num = is_strong(number); |
| 39 | + print_is_armstrong(number, is_strong_num); |
| 40 | + } else { |
| 41 | + print_invalid_input(); |
| 42 | + } |
| 43 | + // Test runs |
| 44 | + // First test, normal values |
| 45 | + test(145, 1, 1); |
| 46 | + // Normal value, false result |
| 47 | + test(15, 0, 2); |
| 48 | + // Min value |
| 49 | + test(0, 1, 3); |
| 50 | + // One |
| 51 | + test(1, 1, 4); |
| 52 | + // Big number |
| 53 | + test(40585, 1, 5); |
| 54 | + // Max value, false result |
| 55 | + test(2147483647, 0, 6); |
| 56 | + return 0; |
| 57 | +} |
| 58 | + |
| 59 | +// 0 - false, 1 - true |
| 60 | +void test(int number, int expected, int test_number) { |
| 61 | + int actual = is_strong(number); |
| 62 | + if (actual == expected) { |
| 63 | + printf("Test #%d: number = %d, expected = %d, actual = %d\nSuccess\n", |
| 64 | + test_number, number, expected, actual); |
| 65 | + } else { |
| 66 | + printf("Test #%d: number = %d, expected = %d, actual = %d\nFailed\n", |
| 67 | + test_number, number, expected, actual); |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +void print_is_armstrong(int number, int is_strong) { |
| 72 | + if (is_strong) { |
| 73 | + printf("%d is a Strong number.\n", number); |
| 74 | + } else { |
| 75 | + printf("%d is not a Strong number.\n", number); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +int is_strong(int number) { |
| 80 | + int is_strong = 0, result = 0, temp_number = number, |
| 81 | + one_digit, number_of_digits = find_number_of_digits(number); |
| 82 | + for (int i = number_of_digits; i > 0; --i) { |
| 83 | + one_digit = temp_number / (1 * power(10, i - 1)); |
| 84 | + temp_number -= one_digit * (1 * power(10, i - 1)); |
| 85 | + result += find_factorial(one_digit); |
| 86 | + } |
| 87 | + if (number == result) { |
| 88 | + is_strong = 1; |
| 89 | + } |
| 90 | + return is_strong; |
| 91 | +} |
| 92 | + |
| 93 | +int find_factorial(int number) { |
| 94 | + int result = 1; |
| 95 | + for (int i = 1; i <= number; i++) { |
| 96 | + result *= i; |
| 97 | + } |
| 98 | + return result; |
| 99 | +} |
| 100 | + |
| 101 | +int find_number_of_digits(int number) { |
| 102 | + int result = 0; |
| 103 | + for (int i = number; i > 0; i /= 10) { |
| 104 | + ++result; |
| 105 | + } |
| 106 | + return result; |
| 107 | +} |
| 108 | + |
| 109 | +int power(int base, int exponent) { |
| 110 | + int result = base; |
| 111 | + if (exponent != 0) { |
| 112 | + for (int i = 2; i <= exponent; i++) { |
| 113 | + result *= base; |
| 114 | + } |
| 115 | + } else { |
| 116 | + result = 1; |
| 117 | + } |
| 118 | + return result; |
| 119 | +} |
| 120 | + |
| 121 | +void print_invalid_input() { |
| 122 | + printf("n/a"); |
| 123 | +} |
| 124 | + |
| 125 | +int input_number() { |
| 126 | + int number; |
| 127 | + char endline; |
| 128 | + printf("Input a number to check whether it is a Strong number:\n"); |
| 129 | + if (!scanf("%d%c", &number, &endline) || endline != '\n') { |
| 130 | + number = -1; |
| 131 | + } |
| 132 | + return number; |
| 133 | +} |
0 commit comments