-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.cpp
167 lines (156 loc) · 3.52 KB
/
Functions.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
#define _USE_MATH_DEFINES
void triangle1(int num);
void triangle2(int num);
int digits_sum(int num);
void natural3nums(int num);
double sinus(double x);
int neastprime(int n);
void main() {
int option, num;
double doubleNum;
cout << "Menu:\n\n0. Exit\n1. Display triangle1\n2. Display triangle2\n3. sum of digits\n4. sum of 3 natural numbers\n5.sinus\n6.neastprime\n\n";
do {
cout << "Enter your choice (0-6):" << endl;
cin >> option;
if(option == 1) {
cin >> num;
triangle1(num);
}
if(option == 2) {
cin >> num;
triangle2(num);
}
if(option == 3) {
cin >> num;
cout << digits_sum(num) << endl;;
}
if(option == 4) {
cin >> num;
natural3nums(num);
}
if(option == 5) {
cin >> doubleNum;
cout << sinus(doubleNum) << endl;
}
if(option == 6) {
cin >> num;
cout << neastprime(num) << endl;
}
}
while(option != 0);
}
//A program that shows a "silver triangle" whose number of rows and columns is the number it receives and its shape is ◣
void triangle1(int num) {
string triang = "";
int temp = 1;
for(int i = num; i > 0; i --) {
for(int j = temp; j > 0; j --)
triang += "*";
triang += "\n";
temp++;
}
cout << triang;
}
//A program that shows a "silver triangle" whose number of rows and columns is the number it receives and its shape is ◤
void triangle2(int num) {
string triang = "";
int temp = num;
for(int i = 1; i <= num; i ++) {
for(int j = 0; j < temp; j ++)
triang += "*";
triang += "\n";
temp--;
}
cout << triang;
}
//A program that returns the sum of the digits of the input number
int digits_sum(int num) {
int sum = 0;
int x;
while(num > 0) {
x = num % 10;
num = num / 10;
sum += x;
}
return sum;
}
//A program that shows all the options to put together the number it gets
void natural3nums(int num) {
string str;
for(int i = 1; i < num; i++) {
for(int j = 1; j < num; j++) {
for(int k = 1; k < num; k++) {
if((i + j + k == num) && (i != j) && (i != k) && (j != k) && (j > i) && (k > j)) {
str = std::to_string(i) + "+" + std::to_string(j) + "+" + std::to_string(k);
cout << str << endl;
}
}
}
}
}
//A program that returns a sin of the input number using the Taylor column
double sinus(double x) {
double M_PI = 3.14;
int flag = 0;
double sinx = 0, factorial;
while((x > (2 * M_PI)) || (x < (-2 * M_PI))) {
if(x > (2 * M_PI))
x = x - 2 * M_PI;
else
x = x + 2 * M_PI;
}
for(int i = 1; i <= 21; i = i + 2) {
factorial = 1;
for(int j = 1; j <= i; j ++)
factorial = factorial * j;
if(flag == 0) {
sinx = sinx + (pow(x, i) / factorial);
flag = 1;
}
else {
sinx = sinx - (pow(x, i) / factorial);
flag = 0;
}
}
return sinx;
}
//A program that receives a natural number, and returns the closest prime number to it
int neastprime(int n) {
int result = 0;
int prime = 1;
int flag = 0;
int prime2 = 1;
for(int i = n; i > 1; i--) {
flag = 0;
for(int j = 2; j < i; j++) {
if((i % j == 0) && (i != j)) {
flag = 1;
}
}
if(flag == 0) {
prime = i;
break;
}
}
for(int i = n; i >= n; i++) {
flag = 0;
for(int j = 2; j < i; j++) {
if((i % j == 0) && (i != j)) {
flag = 1;
}
}
if(flag == 0) {
prime2 = i;
break;
}
}
if((n - prime) <= (prime2 - n))
result = prime;
else
result = prime2;
return result;
}