diff --git a/lesson_04/lesson_04 programs/Prime Number_1.java b/lesson_04/lesson_04 programs/Prime Number_1.java new file mode 100644 index 000000000..caaee39d3 --- /dev/null +++ b/lesson_04/lesson_04 programs/Prime Number_1.java @@ -0,0 +1,36 @@ +import java.util.Scanner; + +public class PrimeChecker { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + + System.out.print("Enter a number: "); + int number = scanner.nextInt(); + + if (isPrime(number)) { + System.out.println(number + " is a prime number."); + } else { + System.out.println(number + " is not a prime number."); + } + + scanner.close(); + } + + public static boolean isPrime(int n) { + if (n <= 1) { + return false; + } + if (n == 2) { + return true; + } + if (n % 2 == 0) { + return false; + } + for (int i = 3; i <= Math.sqrt(n); i += 2) { + if (n % i == 0) { + return false; + } + } + return true; + } +} diff --git a/lesson_04/lesson_04 programs/PrimeNumber.cpp b/lesson_04/lesson_04 programs/PrimeNumber.cpp new file mode 100644 index 000000000..fe18d81e0 --- /dev/null +++ b/lesson_04/lesson_04 programs/PrimeNumber.cpp @@ -0,0 +1,64 @@ +#include +#include +#include + +using namespace std; + +// Function to check if a number is prime +bool isPrime(int num) { + if (num <= 1) return false; + if (num == 2) return true; + if (num % 2 == 0) return false; + + int sqrtNum = sqrt(num); + for (int i = 3; i <= sqrtNum; i += 2) { + if (num % i == 0) return false; + } + return true; +} + +// Function to generate prime numbers up to a limit +vector generatePrimes(int limit) { + vector primes; + for (int i = 2; i <= limit; i++) { + if (isPrime(i)) { + primes.push_back(i); + } + } + return primes; +} + +int main() { + int choice; + cout << "Prime Number Tool\n"; + cout << "1. Check if a number is prime\n"; + cout << "2. Generate prime numbers up to a limit\n"; + cout << "Enter your choice: "; + cin >> choice; + + if (choice == 1) { + int num; + cout << "Enter a number: "; + cin >> num; + if (isPrime(num)) + cout << num << " is a prime number.\n"; + else + cout << num << " is NOT a prime number.\n"; + } + else if (choice == 2) { + int limit; + cout << "Generate primes up to: "; + cin >> limit; + vector primes = generatePrimes(limit); + cout << "Prime numbers up to " << limit << ": "; + for (int p : primes) { + cout << p << " "; + } + cout << "\n"; + } + else { + cout << "Invalid choice.\n"; + } + + return 0; +} \ No newline at end of file diff --git a/lesson_04/lesson_04 programs/README b/lesson_04/lesson_04 programs/README new file mode 100644 index 000000000..53e21dc34 --- /dev/null +++ b/lesson_04/lesson_04 programs/README @@ -0,0 +1,10 @@ +# Java vs. C++: Prime Numbers + +## Introduction + I have compared and contrasted the different ways in how the programming languages Java and C++ claculate whether a number is prime or not and give an appropriate output. + +## Comparisons + Both Java and C++ both use the same algorithm to get to the solution of whether or not a number is prime. Both of these languages have a similar function design. They also have a similar input handling and read an integer from the console and output result to the console as well. + +## Differences + There are many ways in which these two languages differ in the aspect of finding prime numbers. One point of difference is that C++ uses the int main() function and Java uses public static void main(String[] args) as an entry point. Another difference is their data types. For instance, C++ utilizes bool, which is a native type and Java utilizes boolean, which is a primitive. diff --git a/lesson_05/quiz/src/lesson5.ts b/lesson_05/quiz/src/lesson5.ts index 9ad62bd67..3cc7fb052 100644 --- a/lesson_05/quiz/src/lesson5.ts +++ b/lesson_05/quiz/src/lesson5.ts @@ -38,7 +38,7 @@ export class Lesson5 { [AnswerChoice.C, "To insert an image"], [AnswerChoice.D, "To create a paragraph"], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.B, ); } @@ -52,7 +52,7 @@ export class Lesson5 { [AnswerChoice.C, "alt"], [AnswerChoice.D, "href"], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.C, ); } @@ -66,7 +66,7 @@ export class Lesson5 { [AnswerChoice.C, "
"], [AnswerChoice.D, ""], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.A, ); } @@ -80,7 +80,7 @@ export class Lesson5 { [AnswerChoice.C, ""], [AnswerChoice.D, "
"], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.C, ); } @@ -94,7 +94,7 @@ export class Lesson5 { [AnswerChoice.C, "Computer Style Sheets"], [AnswerChoice.D, "Cascading System Sheets"], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.B, ); } @@ -108,7 +108,7 @@ export class Lesson5 { [AnswerChoice.C, "text-color"], [AnswerChoice.D, "background-color"], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.C, ); } @@ -122,7 +122,7 @@ export class Lesson5 { [AnswerChoice.C, "/* this is a comment */"], [AnswerChoice.D, ""], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.C, ); } @@ -136,7 +136,7 @@ export class Lesson5 { [AnswerChoice.C, "text-size"], [AnswerChoice.D, "text-style"], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.B, ); } @@ -150,7 +150,7 @@ export class Lesson5 { [AnswerChoice.C, "inline-block"], [AnswerChoice.D, "none"], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.A, ); } @@ -164,7 +164,7 @@ export class Lesson5 { [AnswerChoice.C, ""], [AnswerChoice.D, ""], ]), - AnswerChoice.UNANSWERED, + AnswerChoice.A, ); } }