diff --git a/08_functions.md b/08_functions.md index f5c646c..3e4dce9 100644 --- a/08_functions.md +++ b/08_functions.md @@ -202,6 +202,139 @@ int multiply(int x, int y) { ``` --- +/* + * gcd_lcm_functions.c + * Demonstrates writing and reusing functions in C. + * + * What it shows: + * - Pure functions with clear inputs/outputs + * - Reuse: LCM computed using GCD + * + * Build: gcc gcd_lcm_functions.c -o gcd_lcm + * Run: ./gcd_lcm + */ + +#include + +int gcd(int a, int b) { + while (b != 0) { + int r = a % b; + a = b; + b = r; + } + return (a < 0) ? -a : a; // handle negatives +} + +long long lcm(int a, int b) { + int g = gcd(a, b); + // avoid overflow for small inputs by casting + return (long long)a / g * b; +} + +int main(void) { + int x, y; + printf("Enter two integers: "); + if (scanf("%d %d", &x, &y) != 2) { + printf("Invalid input.\n"); + return 1; + } + + printf("GCD(%d, %d) = %d\n", x, y, gcd(x, y)); + printf("LCM(%d, %d) = %lld\n", x, y, lcm(x, y)); + return 0; +} + +/* + * perfect_number_function.c + * Checks whether a number is a perfect number using a helper function. + * + * What it shows: + * - Decomposing logic into a function (sum of proper divisors) + * - Simple loop + conditionals inside a function + * + * Build: gcc perfect_number_function.c -o perfect + * Run: ./perfect + */ + +#include + +int sumOfProperDivisors(int n) { + if (n <= 1) return 0; + int sum = 1; // 1 is a proper divisor for n > 1 + for (int i = 2; i * i <= n; i++) { + if (n % i == 0) { + sum += i; + if (i != n / i) sum += n / i; // add the paired divisor + } + } + return sum; +} + +int main(void) { + int n; + printf("Enter a positive integer: "); + if (scanf("%d", &n) != 1 || n <= 0) { + printf("Invalid input.\n"); + return 1; + } + + if (sumOfProperDivisors(n) == n) + printf("%d is a Perfect Number\n", n); + else + printf("%d is NOT a Perfect Number\n", n); + + return 0; +} + +/* + * strong_number_function.c + * Strong number: sum of factorials of digits equals the number (e.g., 145). + * + * What it shows: + * - Multiple small functions (digit factorial, checker) + * - Reusability of a precomputed factorial(0..9) table + * + * Build: gcc strong_number_function.c -o strong + * Run: ./strong + */ + +#include + +int fact[10]; + +void precomputeFactorials(void) { + fact[0] = 1; + for (int d = 1; d <= 9; d++) fact[d] = fact[d - 1] * d; +} + +int isStrong(int n) { + int original = n, sum = 0; + while (n > 0) { + int digit = n % 10; + sum += fact[digit]; + n /= 10; + } + return sum == original; +} + +int main(void) { + precomputeFactorials(); + + int n; + printf("Enter a positive integer: "); + if (scanf("%d", &n) != 1 || n <= 0) { + printf("Invalid input.\n"); + return 1; + } + + if (isStrong(n)) + printf("%d is a Strong Number\n", n); + else + printf("%d is NOT a Strong Number\n", n); + + return 0; +} + ## Summary diff --git a/21_pattern_examples.md b/21_pattern_examples.md index 002e353..9859b56 100644 --- a/21_pattern_examples.md +++ b/21_pattern_examples.md @@ -3,7 +3,7 @@
**🧭 Navigation** -[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md) | [➡️ Next](21_pattern_examples.md) +[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md) | [➡️ Next](README.md)
@@ -256,6 +256,152 @@ int main() { --- +**Code:** +Runnable source: [contributions/basic_programs/pattern_hollow_square.c](../contributions/basic_programs/pattern_hollow_square.c) + +```c +/* + * Hollow Square Pattern + * + * Explanation: + * - The pattern prints a square of size n. + * - Only the border (first row, last row, first column, last column) + * is printed with '*'. + * - The inner cells are printed with spaces to make the square hollow. + + */ + +#include + +int main() { + int n = 5; + + for (int i = 1; i <= n; i++) { + for (int j = 1; j <= n; j++) { + if (i == 1 || i == n || j == 1 || j == n) + printf("* "); + else + printf(" "); + + } + printf("\n"); + } + return 0; +} +``` + +**Expected output:** +``` +* * * * * +* * +* * +* * +* * * * * +``` +--- + + **Code:** +Runnable source: [contributions/basic_programs/pattern_diamond.c](../contributions/basic_programs/pattern_diamond.c) + +```c +/* + * Diamond Star Pattern + * + * Explanation: + * - The diamond is created using: + * 1. Upper pyramid + * 2. Lower inverted pyramid + * - Each row is centered using spaces. + */ + +#include + +int main() { + int n = 5; + + // Upper half + for (int i = 1; i <= n; i++) { + for (int s = 1; s <= n - i; s++) printf(" "); + for (int j = 1; j <= 2*i - 1; j++) printf("*"); + printf("\n"); + } + + // Lower half + for (int i = n - 1; i >= 1; i--) { + for (int s = 1; s <= n - i; s++) printf(" "); + for (int j = 1; j <= 2*i - 1; j++) printf("*"); + printf("\n"); + } + + return 0; +} +``` + +**Expected output:** +``` + * + *** + ***** + ******* +********* + ******* + ***** + *** + * +``` + +--- + + **Code:** +Runnable source: [contributions/basic_programs/pattern_hollow_pyramid.c](../contributions/basic_programs/pattern_hollow_pyramid.c) + +```c +/* + * Hollow Pyramid Pattern + * + * Explanation: + * - Spaces center the pyramid. + * - First and last positions print stars. + * - Last row prints all stars. + */ + +#include + +int main() { + int n = 5; + + for (int i = 1; i <= n; i++) { + + // Leading spaces + for (int s = 1; s <= n - i; s++) printf(" "); + + for (int j = 1; j <= 2*i - 1; j++) { + if (j == 1 || j == 2*i - 1 || i == n) + printf("*"); + else + printf(" "); + + } + + printf("\n"); + } + + return 0; +} +``` + +**Expected output:** +``` + * + * * + * * + * * +********* +``` +--- + + + ## Summary - Pattern programs use nested loops for rows and columns. @@ -267,6 +413,7 @@ int main() {
**🧭 Navigation** -[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md) | [➡️ Next](21_pattern_examples.md) +[⬅️ Previous](20_typedef.md) | [🏠 Home](README.md) +
\ No newline at end of file