Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: new funny algorithms #1327

Closed
wants to merge 8 commits into from
36 changes: 36 additions & 0 deletions Basic Calculator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>

int main() {
char op;
double num1, num2;

std::cout << "Enter an operator (+, -, *, /): ";
std::cin >> op;

std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;

switch (op) {
case '+':
std::cout << num1 + num2 << std::endl;
break;
case '-':
std::cout << num1 - num2 << std::endl;
break;
case '*':
std::cout << num1 * num2 << std::endl;
break;
case '/':
if (num2 != 0) {
std::cout << num1 / num2 << std::endl;
} else {
std::cout << "Error: Division by zero!" << std::endl;
}
break;
default:
std::cout << "Invalid operator" << std::endl;
break;
}

return 0;
}
28 changes: 28 additions & 0 deletions Number-Game.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <cstdlib>
#include <ctime>

int main() {
std::srand(static_cast<unsigned>(std::time(0))); // Seed the random number generator
int secretNumber = std::rand() % 100 + 1; // Generate a random number between 1 and 100
int guess;
int attempts = 0;

std::cout << "Welcome to the Guess the Number game!" << std::endl;

do {
std::cout << "Enter your guess: ";
std::cin >> guess;
attempts++;

if (guess < secretNumber) {
std::cout << "Too low! Try again." << std::endl;
} else if (guess > secretNumber) {
std::cout << "Too high! Try again." << std::endl;
} else {
std::cout << "Congratulations! You guessed the number in " << attempts << " attempts." << std::endl;
}
} while (guess != secretNumber);

return 0;
}
27 changes: 27 additions & 0 deletions funnyalgo.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

int main() {
// Seed the random number generator with the current time
std::srand(std::time(0));

// Create an array of jokes
std::vector<std::string> jokes = {
"Why don't scientists trust atoms? Because they make up everything!",
"Parallel lines have so much in common. It’s a shame they’ll never meet.",
"I told my wife she was drawing her eyebrows too high. She looked surprised.",
"Why don't skeletons fight each other? They don't have the guts.",
"I used to play piano by ear, but now I use my hands."
};

// Generate a random index to select a joke
int randomIndex = std::rand() % jokes.size();

// Display the random joke
std::cout << "Here's a random joke for you:\n";
std::cout << jokes[randomIndex] << std::endl;

return 0;
}
16 changes: 16 additions & 0 deletions star-pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <iostream>

int main() {
int rows;
std::cout << "Enter the number of rows: ";
std::cin >> rows;

for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
std::cout << "* ";
}
std::cout << std::endl;
}

return 0;
}