-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwincalc.h
More file actions
148 lines (128 loc) · 2.89 KB
/
Copy pathwincalc.h
File metadata and controls
148 lines (128 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define PI 3.14159265358979323846 // Precise value of π
// Convert degrees to radians
double deg_to_rad(double degrees) {
return degrees * (PI / 180.0);
}
// Trigonometric functions that accept degrees
double sin_deg(double degrees) {
return sin(deg_to_rad(degrees));
}
double cos_deg(double degrees) {
return cos(deg_to_rad(degrees));
}
double tan_deg(double degrees) {
return tan(deg_to_rad(degrees));
}
// Sum of first n numbers
long int sum_n(long int n) {
return (n * (n + 1)) / 2;
}
// Sum of numbers from [m,n]
long int sum_mn(long int m, long int n) {
return ((n - m + 1) * (m + n)) / 2;
}
// Sum of first n odd numbers
long int sum_odd(long int n) {
return n * n;
}
// Sum of first n even numbers
long int sum_even(long int n) {
return n * (n + 1);
}
// Sum of first n square numbers
long int sum_square(long int n) {
return (n * (n + 1) * (2 * n + 1)) / 6;
}
// Sum of first n cube numbers
long int sum_cube(long int n) {
long int sum = (n * (n + 1)) / 2;
return sum * sum;
}
// Factorial of n (up to 20!)
long long int fact(int n) {
if (n < 0 || n > 20) {
printf("Factorial out of range (0-20)!\n");
return -1;
}
long long int pro = 1;
for (int i = 1; i <= n; i++) {
pro *= i;
}
return pro;
}
// Checking if a number is prime
int isprime(int n) {
if (n < 2) return 0;
for (int i = 2; i * i <= n; i++) { // Optimized loop
if (n % i == 0)
return 0;
}
return 1;
}
// Finding the nth prime number
long int nth_prime(int n) {
long int count = 0, i = 2;
while (count < n) {
if (isprime(i)) count++;
i++;
}
return i - 1;
}
// Printing first n prime numbers
void prime_n(int n) {
int count = 0, i = 2;
while (count < n) {
if (isprime(i)) {
printf("%d ", i);
count++;
}
i++;
}
printf("\n");
}
// Printing prime numbers up to nth prime
void prime_nth(int n) {
for (int i = 2, count = 0; count < n; i++) {
if (isprime(i)) {
printf("%d ", i);
count++;
}
}
printf("\n");
}
// Reversing a number
long int reverse(long int n) {
long int sum = 0;
while (n > 0) {
sum = (sum * 10) + (n % 10);
n /= 10;
}
return sum;
}
// Finding factors of a number
void factors(long int n) {
for (long int i = 1; i <= n; i++) {
if (n % i == 0)
printf("%ld ", i);
}
printf("\n");
}
// Finding even factors
void factors_even(long int n) {
for (long int i = 1; i <= n; i++) {
if (i % 2 == 0 && n % i == 0)
printf("%ld ", i);
}
printf("\n");
}
// Finding odd factors
void factors_odd(long int n) {
for (long int i = 1; i <= n; i++) {
if (i % 2 == 1 && n % i == 0)
printf("%ld ", i);
}
printf("\n");
}