0% found this document useful (0 votes)
13 views22 pages

HAN23080099

Uploaded by

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

HAN23080099

Uploaded by

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

Challenge 1 Variables

Task 1,2,3

#include <iostream>

using namespace std;

int main()

string name = "Khoi";

int age = 18;

double height = 1.65;

char tier = 'Z';

bool subscription = true;

cout << "The player's name is " << name << endl;

cout << "The player's age is " << age << endl;

cout << "The player's height is " << height << endl;

cout << "The player's skill tier is " << tier << endl;

cout << "Subscription status: " << subscription << endl;

//updating variables

name = "Alex";

age = 19;

height = 1.7;

tier = 'A';

subscription = false;

cout << "The player's name is " << name << endl;

cout << "The player's age is " << age << endl;

cout << "The player's height is " << height << endl;

cout << "The player's skill tier is " << tier << endl;

cout << "Subscription status: " << subscription << endl;

return 0;

}
Challenge 2 Loops and Conditionals
Task 1

#include <iostream>

using namespace std;

int main() {

int Input;

cout <<"1.Lion" << endl;

cout <<"2.Dog" << endl;

cout <<"3.Cat" << endl;

cout <<"4.Capybara" << endl;

cout <<"5.Bear" << endl;

cout << "Choose an animal fact(1-5):";

cin >> Input;


switch(Input){

case 1:

cout << "Nearly all wild lions live in Africa, but one small population exists elsewhere" << endl;

break;

case 2:

cout << "Dogs are the most popular pet on the planet!" << endl;

break;

case 3:

cout << "The oldest known pet cat existed 9,500 years ago." << endl;

break;

case 4:

cout << "Capybaras are the largest rodents in the world." << endl;

break;

case 5:

cout << "Bears are big, strong, and fast!" << endl;

break;

default:

cout << "invalid input" << endl;

break;

return 0;

}
Task 2

#include <iostream>

using namespace std;

int main() {

int inputNumber;

cout << "Input a number:";

cin >> inputNumber;

cout << "All even numbers between 2 and " << inputNumber << " are :" << endl;

for (int i=2; i <= inputNumber; i+=2){

cout << i << endl;

return 0;

}
Task 3

#include <iostream>

using namespace std;

int main() {

int base;

int exponent;

bool choice = 1;

//Exponent calculator

while (choice == 1) {

int result = 1;

cout << "Enter a base number:";

cin >> base;

cout << "Enter an exponent number (the number must be lower than or equal to 5):";

cin >> exponent;

if (exponent <= 5){

for (int i=0 ; i < exponent;i++){

result *= base;

cout << base << "^" << exponent << " equals " << result <<endl;
}

else{

cout << "exponent must be lower than or equal to 5" << endl;

break;

cout << "Would you like to go again? (1) Yes or (0) No" << endl;

cin >> choice;

cout << "ending program";

return 0;

Challenge 3 Functions
Task 1

#include <iostream>
using namespace std;

void start(){

cout << "Welcome" << endl;

int subtract(){

int firstNum;

int secondNum;

cout << "Input the first number: ";

cin >> firstNum;

cout << "Input the second number: ";

cin >> secondNum;

int result = firstNum - secondNum;

cout << firstNum << " - " << secondNum << " equals " << result << endl;

return result;

void checkResult(){

if (subtract() < 0){

cout << "The result is negative";

else {

cout << "The result is positive";

int main() {

start();

checkResult();

return 0;
}

Task 2

#include <cstdlib>

#include <iostream>

using namespace std;

int main() {

int lowest;

int highest;

int count;

cout << "type in the lowest number to be generated: ";

cin >> lowest;

cout << "type in the highest number to be generated: ";

cin >> highest;

cout << "how many numbers to generate: ";

cin >> count;

srand(time(NULL));

for (int i=0; i < count; i++ ){

int genNum = lowest + rand() % highest;

cout << genNum << " ";


}

return 0;

Task 3

#include <cstdlib>

#include <iostream>

using namespace std;

//Generate a random number between min and max

int numGen() {

int min = 0;

int max = 30;

srand(time(NULL));

int genNum = min + rand() % max;

return genNum;

//Game's rules

void game(){

int guess;

bool correct = 0;
int answer = numGen();

cout << "Your guess: ";

cin >> guess;

while (correct == 0){

if (guess > answer){

cout << "Try a lower number" << endl;

cin >> guess;

else if (guess < answer){

cout << "Try a higher number" << endl;

cin >> guess;

else {

cout << "correct" << endl;

correct = 1;

int main(){

cout << "Guess a number between 1-30:" << endl;

game();

return 0;

}
Challenge 4 Strings
Task 1

#include <iostream>

using namespace std;

//Questions 1-5

int ask()

string ans1;

cout << "Question 1:" << endl;

cout << "What is the capital city of England?" << endl;

cin >> ans1;

//result
if (ans1 == "London") {

cout << "You're correct" << endl;

return 1;

else {

cout << "You're wrong" << endl;

return 0;

int ask2()

string ans1;

cout << "Question 2:" << endl;

cout << "What is the capital city of Vietnam?" << endl;

cin >> ans1;

if (ans1 == "Hanoi") {

cout << "You're correct" << endl;

return 1;

else {

cout << "You're wrong" << endl;

return 0;

int ask3()

string ans1;
cout << "Question 3:" << endl;

cout << "What is the capital city of Germany?" << endl;

cin >> ans1;

if (ans1 == "Berlin") {

cout << "You're correct" << endl;

return 1;

else {

cout << "You're wrong" << endl;

return 0;

int ask4()

string ans1;

cout << "Question 4:" << endl;

cout << "What is the capital city of Japan?" << endl;

cin >> ans1;

if (ans1 == "Tokyo") {

cout << "You're correct" << endl;

return 1;

else {

cout << "You're wrong" << endl;

return 0;

int ask5()
{

string ans1;

cout << "Question 5:" << endl;

cout << "What is the capital city of Australia?" << endl;

cin >> ans1;

if (ans1 == "Canberra") {

cout << "You're correct" << endl;

return 1;

else {

cout << "You're wrong" << endl;

return 0;

//The Program

int main() {

bool choice = true ;

while (choice == 1) {

if (ask() == 1) {

if (ask2() == 1) {

if (ask3() == 1) {

if (ask4() == 1) {

if (ask5() == 1) {

cout << "All done" << endl;

}
else{}

cout << "Do you want to play again: (1 for yes, 0 for no)" << endl;

cin >> choice;

Task 2

#include <iostream>

#include <cstdlib>
#include <string>

using namespace std;

int main() {

string username;

cout << "Enter your name:";

cin >> username;

//NumberGenerator

srand(time(NULL));

int ranNum = 1000 + rand() % 1001;

username.append("#" + to_string(ranNum));

cout <<"Your username is " << username;

return 0;

Task 3

#include <iostream>

#include <cstdlib>

#include <string>

using namespace std;

int main() {

string userName;

string userNameNew;
cout << "Enter your username:";

cin >> userName;

string profanity = "poop";

//profanity check

if (userName.find(profanity) == -1) {

cout << "You're good to go!";

else {

//check if there is one or more profanity words

while (userName.find(profanity) != -1) {

int check = userName.find(profanity);

userNameNew = userName.replace(check, profanity.length(), "****");

cout << "Illegal username.";

cout << "Your new username is " << userNameNew;

Challenge 5 Program
Task 1

#include<cstdlib>

#include<string>

#include<iostream>

using namespace std;

//public Variables

string sCPUstr;

int sPlayingGame =1;

int sPlayerPoint = 0;

int sCPUPoint = 0;

//random numbers generator

int cpuNum() {

srand(unsigned(time(NULL)));

int cpuNum = 1 + rand() % 3;

return cpuNum;

//Convert random numbers to string

void converter() {

switch (cpuNum()) {

case 1:

sCPUstr = "water";

break;

case 2:

sCPUstr = "fire";

break;

case 3:

sCPUstr = "earth";

break;
}

//The Game's Rules

void game() {

string sInput;

cout << "Choose earth, water or fire:" << endl;

cin >> sInput;

cout << "The computer chose " << sCPUstr << endl;

if (sInput == sCPUstr) {

cout << "Tie" << endl;

else if ((sInput == "earth" && sCPUstr == "water") || (sInput == "fire" && sCPUstr == "earth") ||
(sInput == "water" && sCPUstr == "fire")) {

cout << "You won the round" << endl;

sPlayerPoint++;

else {

cout << "You lost the round" << endl;

sCPUPoint++;

cout << "Your score: " << sPlayerPoint << endl;

cout << "Computer's score: " << sCPUPoint << endl;

//main program

int main() {

while (sPlayingGame == 1) {

cout << "Game started" << endl;

sPlayerPoint = 0;

sCPUPoint = 0;
while ((sPlayerPoint < 3) && (sCPUPoint < 3) ) {

converter();

game();

if (sPlayerPoint == 3) {

cout << "You won the game." << endl;

sPlayingGame = 0;

if (sCPUPoint == 3) {

cout << "You lost the game." << endl;

sPlayingGame = 0;

cout << "Play again? (1) Yes or (0) No" << endl;

cin >> sPlayingGame;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy