Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 133 additions & 0 deletions 08_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>

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;
}
Comment on lines +205 to +245
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

Align comment formatting with documentation standards.

The C code examples in this section use inconsistent formatting with raw markdown comments instead of embedding comments within fenced C code blocks. This violates MD007 and MD018 linting rules.

Restructure the three new examples (gcd_lcm_functions, perfect_number_function, strong_number_function) to place C-style comments inside the fenced code blocks, consistent with the repository's existing documentation:

-/*
- * 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 <stdio.h>
+
+/*
+ * gcd_lcm_functions.c
+ * Demonstrates writing and reusing functions in C.
+ * 
+ * Shows: Pure functions with clear I/O; LCM computed using GCD
+ * Build:  gcc gcd_lcm_functions.c -o gcd_lcm
+ * Run:    ./gcd_lcm
+ */

Apply the same restructuring to the perfect_number_function.c and strong_number_function.c comments.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
/*
* 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 <stdio.h>
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;
}
#include <stdio.h>
/*
* gcd_lcm_functions.c
* Demonstrates writing and reusing functions in C.
*
* Shows: Pure functions with clear I/O; LCM computed using GCD
* Build: gcc gcd_lcm_functions.c -o gcd_lcm
* Run: ./gcd_lcm
*/
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;
}
🧰 Tools
🪛 markdownlint-cli2 (0.18.1)

206-206: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


207-207: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


208-208: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


209-209: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


210-210: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


210-210: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


211-211: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


211-211: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)


212-212: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


213-213: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


214-214: Unordered list indentation
Expected: 0; Actual: 1

(MD007, ul-indent)


217-217: No space after hash on atx style heading

(MD018, no-missing-space-atx)


242-242: Code block style
Expected: fenced; Actual: indented

(MD046, code-block-style)

🤖 Prompt for AI Agents
In 08_functions.md around lines 205 to 245, the example uses raw markdown
comment syntax outside of the fenced C code block which violates MD007/MD018;
update the three examples (gcd_lcm_functions, perfect_number_function,
strong_number_function) so that each C example is entirely inside a ```c fenced
code block and contains C-style comments (/* ... */) at the top of the code
instead of markdown text outside the block; ensure the fence markers surround
only the C source, move any descriptive lines into C comments within the block,
and make the formatting consistent with existing repository examples.


/*
* 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 <stdio.h>

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 <stdio.h>

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

Expand Down
155 changes: 155 additions & 0 deletions 21_pattern_examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>

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 <stdio.h>

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 <stdio.h>

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.
Expand Down