KEMBAR78
Bakerfranke Github io:CodePrint | PDF | Prime Number | Mathematics
0% found this document useful (0 votes)
13 views4 pages

Bakerfranke Github io:CodePrint

Uploaded by

tanmaywrizz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views4 pages

Bakerfranke Github io:CodePrint

Uploaded by

tanmaywrizz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

AP CS Principles - Program Code

Math Utilities

#include <iostream> //for input-output operations


#include <vector> //for using dynamic arrays
#include <cmath> //for mathematical functions(sqrt)
using namespace std;

//declaring functions
void quadsolver(double a, double b, double c); //solves a quadrating equation. a,b,c parameters.
bool isprime(int n); //returns whether a number is prime or not
vector<int> primefactorizer(int num); //finds prime factors of a number
void primenumsbw(int start, int last, int index); //to find prime numbers in a given range

int main() {

//the main menu


cout << "WELCOME TO THE MATH UTILITIES APP!" << endl;
cout << "Select tool : " << endl;
cout << "enter 1 for the basic calculator" << endl;
cout << "enter 2 for the quadratic equation solver" << endl;
cout << "enter 3 for the prime factorizer" << endl;
cout << "enter 4 for the 'prime numbers between' tool" << endl;
cout << "choose the tool : ";

int choice;
cin >> choice; //taking user input for tool choice

//conditional logic
if (choice == 1) {
cout << "Enter the two numbers (e.g., for x - y or x / y, enter x first, then y): ";

int x, y;
cin >> x >> y; //input

cout << "enter the operation (1 for addition, 2 for subtraction, 3 for multiplication, and 4 for

int operation;
cin >> operation; //input for operation choice

//conditional logic
if (operation == 1) {
cout << "the answer is " << x + y << endl; //output for addition
cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
} else if (operation == 2) {
cout << "the answer is " << x - y << endl; //output for subtraction
cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
} else if (operation == 3) {
cout << "the answer is " << x * y << endl; //output for multiplication
cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
} else if (operation == 4) {
if (y == 0) { //handling division by 0
cout << "Invalid Input(can't divide by 0)!" << endl;
} else {
cout << "the quotient is " << x / y << " and the remainder is " << x % y << endl; //outpu
cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
}
} else {
cout << "invalid operation(please enter a number between 1 and 4.)" << endl; //dealing with i
}
} else if (choice == 2) {
cout << "enter the coefficients of the quadratic equation." << endl;
cout << "ax^2 + bx + c = 0" << endl;
double a, b, c;
//input for the coefficients of the equation
cout << "a : ";
cin >> a;

cout << "b : ";


cin >> b;

cout << "c : ";


cin >> c;

if (a == 0) {
cout << "Invalid Input(a can't be 0)!" << endl; //dealing with a = 0(not a quadratic equation
} else {
quadsolver(a, b, c); //outputs the roots
cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
}
} else if (choice == 3) {
cout << "enter n, the number of which you want the prime factorization : ";

int num;
cin >> num;

if (num < 1) {
//check for invalid input
cout << "Invalid input!" << endl;
} else {
vector<int> factorization = primefactorizer(num);
//display the prime factors :
cout << "the prime factorization of " << num << " is : " << factorization[0];

for (int i = 1; i < factorization.size(); i++) {


cout << "*" << factorization[i];
}

cout << endl;


cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
}
} else if (choice == 4) {
cout << "Find prime numbers within a given range." << endl;
cout << "Both the lower and upper bounds are included." << endl;
cout << "You can either:" << endl;
cout << " - Enter 0 to display all prime numbers in the range," << endl;
cout << " - Or enter the position of a specific prime in the range (e.g., 2 will show the second
cout << endl;

int start, last, index;


cout << "Enter the lower bound: ";
cin >> start;//input

cout << "Enter the upper bound: ";


cin >> last;//input

cout << "Enter the position of the prime number to view (0 for full list): ";
cin >> index;//input

if ((start < 1) || (last < 1) || (last <= start) || (index < 0)) {
//dealing with invalid cases
cout << "Invalid Input!" << endl;
} else {
primenumsbw(start, last, index);
cout << "Beep Boop! Operation complete. Thanks for using!" << endl;
}
} else {
//invalid tool choice
cout << "please choose a number between 1 and 4." << endl;
}
}

void quadsolver(double a, double b, double c) {


double root1;
double root2;
double rootim;//imaginary part of the root
double rootrl;//real part of the root

if ((b * b - 4 * a * c) < 0) {
//discriminant < 0, complex roots
rootim = (sqrt(4 * a * c - b * b)) / (2 * a);
rootrl = (-1 * b) / (2 * a);
cout << "the roots are " << rootrl << " + " << rootim << "i and " << rootrl << " - " << rootim <<
} else {
//real root(s)
root1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
root2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);
if(root1 == root2){
//if there's only one root(discriminant = 0)
cout << "the root is " << root1 << endl;
}else{
//discriminant > 0, 2 real roots.
cout << "the roots are " << root1 << " and " << root2 << "." << endl;
}
}
}

vector<int> primefactorizer(int num) {


vector<int> prmfctrs;

while (num % 2 == 0) {
//dividing by 2 until n is odd
prmfctrs.push_back(2);
num = num / 2;
}
//iterating,i+2 as n is not even at this stage :
for (int i = 3; i * i <= num; i = i + 2) {
while (num % i == 0) {
prmfctrs.push_back(i);
num = num / i;
}
}

if (num > 2) {
//in this case, the number left is prime.
prmfctrs.push_back(num);
}

return prmfctrs;
}
//function to check if a number is prime, will be helpful in the "primenumsbw" function.
bool isprime(int n) {
if (n <= 3) {
return true;
}

if (n % 2 == 0 || n % 3 == 0) {
return false;
}

for (int i = 5; i * i <= n; i += 6) {


if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
194
194
195
return true;
196
}
197
198
//function to get prime numbers in a given range.
199
void primenumsbw(int start, int last, int index) {
200
201 vector<int> prmsbw;
202 //find all prime numbers in the range :
for (int currentnum = start; currentnum <= last; currentnum++) {
203
204 if (isprime(currentnum) == true) {

205 prmsbw.push_back(currentnum);
}
206
}
207
//handling the case where no prime is found in the range:
208
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 :" << e
215 cout << prmsbw[0];

216
217 for (int i = 1; i < prmsbw.size(); i++) {
cout << "," << prmsbw[i];
218
219 }

220
221 cout << endl;
}
222
223 //display a specific prime number based on the position :
else {
224
225 //dealing with the case where the index given by the user is greater than the number of primes in the ran

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

You might also like