From 3affda3d90464a7458679bb0032d3e6ac0146b7b Mon Sep 17 00:00:00 2001 From: varun <2410030053@klh.edu.in> Date: Thu, 6 Nov 2025 22:05:16 +0530 Subject: [PATCH 1/3] =?UTF-8?q?Docs:=20add=203=20function=20examples=20(GC?= =?UTF-8?q?D=20&=20LCM,=20Perfect,=20Strong)=20=E2=80=94=20Refs=20#16?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 08_functions.md | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) 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 From 809a2ae0f0416ba39c332f2abf2bf24dc7809144 Mon Sep 17 00:00:00 2001 From: varun <2410030053@klh.edu.in> Date: Thu, 6 Nov 2025 22:27:00 +0530 Subject: [PATCH 2/3] Docs: add 3 new pattern programs (Hollow Square, Diamond, Hollow Pyramid) --- 21_pattern_examples.md | 155 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) diff --git a/21_pattern_examples.md b/21_pattern_examples.md index 002e353..caca7aa 100644 --- a/21_pattern_examples.md +++ b/21_pattern_examples.md @@ -256,6 +256,161 @@ int main() { --- + +/* + * 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++) { + + // Print star on the boundary, space inside + if (i == 1 || i == n || j == 1 || j == n) { + printf("* "); + } else { + printf(" "); + } + } + printf("\n"); + } + return 0; +} +**Expected output:** +``` + * * * * + * * + * * + * * + * * * * +``` +--- + +/* + * Diamond Star Pattern + * + * Explanation: + * - The diamond is created using two pyramids: + * 1. Upper pyramid (increasing stars) + * 2. Lower inverted pyramid (decreasing stars) + * - For each line: + * - First print spaces to center the stars. + * - Then print (2*i - 1) stars to maintain symmetric shape. + + */ + +#include + +int main() { + int n = 5; + + // Upper half of diamond + for (int i = 1; i <= n; i++) { + // Print leading spaces + for (int s = 1; s <= n - i; s++) { + printf(" "); + } + // Print stars + for (int j = 1; j <= 2*i - 1; j++) { + printf("*"); + } + printf("\n"); + } + + // Lower half of diamond + 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:** +``` + + * + *** + ***** + ******* + ********* + ******* + ***** + *** + * +``` +--- + +/* + * Hollow Pyramid Pattern + * + * Explanation: + * - A pyramid with its inside hollow. + * - For each row: + * * Print leading spaces to center the pyramid. + * * Print 1 star at the start and 1 star at the end. + * * For the last row, print all stars to close the shape. + + */ + +#include + +int main() { + int n = 5; + + for (int i = 1; i <= n; i++) { + + // Print leading spaces + for (int s = 1; s <= n - i; s++) { + printf(" "); + } + + // Print hollow pattern + for (int j = 1; j <= 2*i - 1; j++) { + + // First star, last star, or entire last row + 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. From 9b139c2d26fd2a09562b073eac4a12707ab499a4 Mon Sep 17 00:00:00 2001 From: varun <2410030053@klh.edu.in> Date: Sat, 8 Nov 2025 11:38:20 +0530 Subject: [PATCH 3/3] docs: sync pattern examples with .c files and fix code blocks --- 21_pattern_examples.md | 132 +++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 70 deletions(-) diff --git a/21_pattern_examples.md b/21_pattern_examples.md index caca7aa..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,7 +256,10 @@ int main() { --- +**Code:** +Runnable source: [contributions/basic_programs/pattern_hollow_square.c](../contributions/basic_programs/pattern_hollow_square.c) +```c /* * Hollow Square Pattern * @@ -265,9 +268,7 @@ int main() { * - 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 @@ -277,39 +278,40 @@ int main() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { - - // Print star on the boundary, space inside - if (i == 1 || i == n || j == 1 || j == n) { + if (i == 1 || i == n || j == 1 || j == n) printf("* "); - } else { + 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 two pyramids: - * 1. Upper pyramid (increasing stars) - * 2. Lower inverted pyramid (decreasing stars) - * - For each line: - * - First print spaces to center the stars. - * - Then print (2*i - 1) stars to maintain symmetric shape. - + * - The diamond is created using: + * 1. Upper pyramid + * 2. Lower inverted pyramid + * - Each row is centered using spaces. */ #include @@ -317,57 +319,50 @@ int main() { int main() { int n = 5; - // Upper half of diamond + // Upper half for (int i = 1; i <= n; i++) { - // Print leading spaces - for (int s = 1; s <= n - i; s++) { - printf(" "); - } - // Print stars - for (int j = 1; j <= 2*i - 1; j++) { - printf("*"); - } + for (int s = 1; s <= n - i; s++) printf(" "); + for (int j = 1; j <= 2*i - 1; j++) printf("*"); printf("\n"); } - // Lower half of diamond + // 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("*"); - } + 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: - * - A pyramid with its inside hollow. - * - For each row: - * * Print leading spaces to center the pyramid. - * * Print 1 star at the start and 1 star at the end. - * * For the last row, print all stars to close the shape. - + * - Spaces center the pyramid. + * - First and last positions print stars. + * - Last row prints all stars. */ #include @@ -377,20 +372,15 @@ int main() { for (int i = 1; i <= n; i++) { - // Print leading spaces - for (int s = 1; s <= n - i; s++) { - printf(" "); - } + // Leading spaces + for (int s = 1; s <= n - i; s++) printf(" "); - // Print hollow pattern for (int j = 1; j <= 2*i - 1; j++) { - - // First star, last star, or entire last row - if (j == 1 || j == 2*i - 1 || i == n) { + if (j == 1 || j == 2*i - 1 || i == n) printf("*"); - } else { + else printf(" "); - } + } printf("\n"); @@ -398,14 +388,15 @@ int main() { return 0; } -**Expected output:** ``` - * - * * - * * - * * - ********* +**Expected output:** +``` + * + * * + * * + * * +********* ``` --- @@ -422,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