Skip to content
Closed

Quiz #154

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions lesson_04/lesson_04 programs/Prime Number_1.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
64 changes: 64 additions & 0 deletions lesson_04/lesson_04 programs/PrimeNumber.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include <iostream>
#include <vector>
#include <cmath>

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<int> generatePrimes(int limit) {
vector<int> 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<int> primes = generatePrimes(limit);
cout << "Prime numbers up to " << limit << ": ";
for (int p : primes) {
cout << p << " ";
}
cout << "\n";
}
else {
cout << "Invalid choice.\n";
}

return 0;
}
10 changes: 10 additions & 0 deletions lesson_04/lesson_04 programs/README
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 10 additions & 10 deletions lesson_05/quiz/src/lesson5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Lesson5 {
[AnswerChoice.C, "To insert an image"],
[AnswerChoice.D, "To create a paragraph"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.B,
);
}

Expand All @@ -52,7 +52,7 @@ export class Lesson5 {
[AnswerChoice.C, "alt"],
[AnswerChoice.D, "href"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.C,
);
}

Expand All @@ -66,7 +66,7 @@ export class Lesson5 {
[AnswerChoice.C, "<div>"],
[AnswerChoice.D, "<link>"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.A,
);
}

Expand All @@ -80,7 +80,7 @@ export class Lesson5 {
[AnswerChoice.C, "<span>"],
[AnswerChoice.D, "<br>"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.C,
);
}

Expand All @@ -94,7 +94,7 @@ export class Lesson5 {
[AnswerChoice.C, "Computer Style Sheets"],
[AnswerChoice.D, "Cascading System Sheets"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.B,
);
}

Expand All @@ -108,7 +108,7 @@ export class Lesson5 {
[AnswerChoice.C, "text-color"],
[AnswerChoice.D, "background-color"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.C,
);
}

Expand All @@ -122,7 +122,7 @@ export class Lesson5 {
[AnswerChoice.C, "/* this is a comment */"],
[AnswerChoice.D, "<!-- this is a comment -->"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.C,
);
}

Expand All @@ -136,7 +136,7 @@ export class Lesson5 {
[AnswerChoice.C, "text-size"],
[AnswerChoice.D, "text-style"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.B,
);
}

Expand All @@ -150,7 +150,7 @@ export class Lesson5 {
[AnswerChoice.C, "inline-block"],
[AnswerChoice.D, "none"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.A,
);
}

Expand All @@ -164,7 +164,7 @@ export class Lesson5 {
[AnswerChoice.C, "<stylesheet link='styles.css'>"],
[AnswerChoice.D, "<css href='styles.css'>"],
]),
AnswerChoice.UNANSWERED,
AnswerChoice.A,
);
}
}
Expand Down
Loading