Skip to content

Commit 5a6d50b

Browse files
committed
moved some snippets from C++ to C
1 parent f95b751 commit 5a6d50b

File tree

5 files changed

+18
-24
lines changed

5 files changed

+18
-24
lines changed

snippets/cpp/math-and-numbers/check-perfect-number.md renamed to snippets/c/mathematical-functions/check-perfect-number.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,24 @@ tags: math, perfect-number
55
author: ashukr07
66
---
77

8-
```cpp
8+
```c
9+
#include <stdbool.h>
10+
11+
// Function to check if a number is a perfect number
912
bool is_perfect(int n) {
13+
if (n <= 1) return false;
14+
1015
int sum = 1; // 1 is a divisor for all n > 1
1116
for (int i = 2; i * i <= n; ++i) {
1217
if (n % i == 0) {
1318
sum += i;
1419
if (i != n / i) sum += n / i;
1520
}
1621
}
17-
return sum == n && n != 1;
22+
return sum == n;
1823
}
1924

20-
// Usage:
25+
// Usage
2126
is_perfect(28); // Returns: true
27+
is_perfect(12); // Returns: false
2228
```

snippets/cpp/math-and-numbers/compound-interest.md renamed to snippets/c/mathematical-functions/compound-interest.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ tags: math, finance
55
author: ashukr07
66
---
77

8-
```cpp
9-
#include <cmath>
8+
```c
9+
#include <math.h>
1010

11+
// Function to calculate compound interest
1112
double compound_interest(double principal, double rate, double time, int n) {
12-
return principal * std::pow(1 + rate / n, n * time);
13+
return principal * pow(1 + rate / n, n * time);
1314
}
1415

1516
// Usage:
1617
double principal = 1000.0; // Initial amount
1718
double rate = 0.05; // Annual interest rate (5%)
1819
double time = 2; // Time in years
1920
int n = 4; // Compounded quarterly
21+
2022
compound_interest(principal, rate, time, n); // Returns: 1104.081632653061
2123
```

snippets/cpp/math-and-numbers/fibonacci-number.md renamed to snippets/c/mathematical-functions/fibonacci-number.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ tags: math, fibonacci, recursion
55
author: ashukr07
66
---
77

8-
```cpp
8+
```c
9+
// Function to calculate the nth Fibonacci number
910
int fibonacci(int n) {
1011
if (n <= 1) return n;
1112
return fibonacci(n - 1) + fibonacci(n - 2);

snippets/cpp/math-and-numbers/sum-of-digits.md renamed to snippets/c/mathematical-functions/sum-of-digits.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ tags: math, digits
55
author: ashukr07
66
---
77

8-
```cpp
8+
```c
9+
// Function to calculate the sum of the digits of an integer
910
int sum_of_digits(int n) {
1011
int sum = 0;
1112
while (n != 0) {

snippets/cpp/math-and-numbers/factorial.md

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)