30/04/2025, 21:35 bakerfranke.github.
io/codePrint/
AP Comp Sci Principles - Create Task, Program Code
MATH UTILITIES
1 #include <iostream> //for input-output operations
2 #include <vector> //for using dynamic arrays
3 #include <cmath> //for mathematical functions(sqrt)
4 using namespace std;
5
6 //declaring functions
7 void quadsolver(double a, double b, double c); //solves a quadrating equation. a,b,c parameters.
8 bool isprime(int n); //returns whether a number is prime or not
9 vector<int> primefactorizer(int num); //finds prime factors of a number
10 void primenumsbw(int start, int last, int index); //to find prime numbers in a given range
11
12 int main() {
13
14 //the main menu
15 cout << "WELCOME TO THE MATH UTILITIES APP!" << endl;
16 cout << "Select tool : " << endl;
17 cout << "enter 1 for the basic calculator" << endl;
18 cout << "enter 2 for the quadratic equation solver" << endl;
19 cout << "enter 3 for the prime factorizer" << endl;
20 cout << "enter 4 for the 'prime numbers between' tool" << endl;
21 cout << "choose the tool : ";
22
23 int choice;
24 cin >> choice; //taking user input for tool choice
25
26 //conditional logic
27 if (choice == 1) {
28 cout << "Enter the two numbers (e.g., for x - y or x / y, enter x first, then y): ";
29
30 int x, y;
31 cin >> x >> y; //input
32
33 cout << "enter the operation (1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division) : ";
34
35 int operation;
36 cin >> operation; //input for operation choice
37
38 //conditional logic
39 if (operation == 1) {
40 cout << "the answer is " << x + y << endl; //output for addition
41 cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
42 } else if (operation == 2) {
43 cout << "the answer is " << x - y << endl; //output for subtraction
44 cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
45 } else if (operation == 3) {
46 cout << "the answer is " << x * y << endl; //output for multiplication
47 cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
48 } else if (operation == 4) {
49 if (y == 0) { //handling division by 0
50 cout << "Invalid Input(can't divide by 0)!" << endl;
51 } else {
52 cout << "the quotient is " << x / y << " and the remainder is " << x % y << endl; //output for division
53 cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
54 }
55 } else {
56 cout << "invalid operation(please enter a number between 1 and 4.)" << endl; //dealing with invalid operation choice
57 }
58 } else if (choice == 2) {
59 cout << "enter the coefficients of the quadratic equation." << endl;
60 cout << "ax^2 + bx + c = 0" << endl;
61
62 double a, b, c;
63 //input for the coefficients of the equation
64 cout << "a : ";
65 cin >> a;
66
67 cout << "b : ";
68 cin >> b;
69
70 cout << "c : ";
71 cin >> c;
72
73 if (a == 0) {
74 cout << "Invalid Input(a can't be 0)!" << endl; //dealing with a = 0(not a quadratic equation)
75 } else {
76 quadsolver(a, b, c); //outputs the roots
77 cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
78 }
79 } else if (choice == 3) {
80 cout << "enter n, the number of which you want the prime factorization : ";
81
82 int num;
83 cin >> num;
84
https://bakerfranke.github.io/codePrint/ 1/3
30/04/2025, 21:35 bakerfranke.github.io/codePrint/
85 if (num < 1) {
86 //check for invalid input
87 cout << "Invalid input!" << endl;
88 } else {
89 vector<int> factorization = primefactorizer(num);
90 //display the prime factors :
91 cout << "the prime factorization of " << num << " is : " << factorization[0];
92
93 for (int i = 1; i < factorization.size(); i++) {
94 cout << "*" << factorization[i];
95 }
96
97 cout << endl;
98 cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
99 }
100 } else if (choice == 4) {
101 cout << "Find prime numbers within a given range." << endl;
102 cout << "Both the lower and upper bounds are included." << endl;
103 cout << "You can either:" << endl;
104 cout << " - Enter 0 to display all prime numbers in the range," << endl;
105 cout << " - Or enter the position of a specific prime in the range (e.g., 2 will show the second prime)." << endl;
106 cout << endl;
107
108 int start, last, index;
109 cout << "Enter the lower bound: ";
110 cin >> start;//input
111
112 cout << "Enter the upper bound: ";
113 cin >> last;//input
114
115 cout << "Enter the position of the prime number to view (0 for full list): ";
116 cin >> index;//input
117
118 if ((start < 1) || (last < 1) || (last <= start) || (index < 0)) {
119 //dealing with invalid cases
120 cout << "Invalid Input!" << endl;
121 } else {
122 primenumsbw(start, last, index);
123 cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
124 }
125 } else {
126 //invalid tool choice
127 cout << "please choose a number between 1 and 4." << endl;
128 }
129 }
130
131 void quadsolver(double a, double b, double c) {
132 double root1;
133 double root2;
134 double rootim;//imaginary part of the root
135 double rootrl;//real part of the root
136
137 if ((b * b - 4 * a * c) < 0) {
138 //discriminant < 0, complex roots
139 rootim = (sqrt(4 * a * c - b * b)) / (2 * a);
140 rootrl = (-1 * b) / (2 * a);
141 cout << "the roots are " << rootrl << " + " << rootim << "i and " << rootrl << " - " << rootim << "i" << endl;
142 } else {
143 //real root(s)
144 root1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
145 root2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
146 if(root1 == root2){
147 //if there's only one root(discriminant = 0)
148 cout << "the root is " << root1 << endl;
149 }else{
150 //discriminant > 0, 2 real roots.
151 cout << "the roots are " << root1 << " and " << root2 << "." << endl;
152 }
153 }
154 }
155
156 vector<int> primefactorizer(int num) {
157 vector<int> prmfctrs;
158
159 while (num % 2 == 0) {
160 //dividing by 2 until n is odd
161 prmfctrs.push_back(2);
162 num = num / 2;
163 }
164 //iterating,i+2 as n is not even at this stage :
165 for (int i = 3; i * i <= num; i = i + 2) {
166 while (num % i == 0) {
167 prmfctrs.push_back(i);
168 num = num / i;
169 }
170 }
171
172 if (num > 2) {
173 //in this case, the number left is prime.
174 prmfctrs.push_back(num);
175
https://bakerfranke.github.io/codePrint/ 2/3
30/04/2025, 21:35 bakerfranke.github.io/codePrint/
176 }
177
178 return prmfctrs;
179 }
180 //function to check if a number is prime, will be helpful in the "primenumsbw" function.
181 bool isprime(int n) {
182 if (n <= 3) {
183 return true;
184 }
185
186 if (n % 2 == 0 || n % 3 == 0) {
187 return false;
188 }
189
190 for (int i = 5; i * i <= n; i += 6) {
191 if (n % i == 0 || n % (i + 2) == 0) {
192 return false;
193 }
194 }
195
196 return true;
197 }
198
199 //function to get prime numbers in a given range.
200 void primenumsbw(int start, int last, int index) {
201 vector<int> prmsbw;
202 //find all prime numbers in the range :
203 for (int currentnum = start; currentnum <= last; currentnum++) {
204 if (isprime(currentnum) == true) {
205 prmsbw.push_back(currentnum);
206 }
207 }
208 //handling the case where no prime is found in the range:
209 if (prmsbw.size() == 0) {
210 cout << "No prime number in this range!" << endl;
211 }
212 //display full list of prime numbers :
213 else if (index == 0) {
214 cout << "The complete list of prime numbers between " << start << " and " << last << " is :" << endl;
215 cout << prmsbw[0];
216
217 for (int i = 1; i < prmsbw.size(); i++) {
218 cout << "," << prmsbw[i];
219 }
220
221 cout << endl;
222 }
223 //display a specific prime number based on the position :
224 else {
225 //dealing with the case where the index given by the user is greater than the number of primes in the range :
226 if (index > prmsbw.size()) {
227 cout << "Error : Position exceeds number of primes in this range." << endl;
228 } else {
229 index = index - 1;
230 cout << "The prime number at position " << index + 1 << " is " << prmsbw[index] << endl;
231 }
232 }
}
PDF document made with CodePrint using Prism
https://bakerfranke.github.io/codePrint/ 3/3