Tutorial 4 C++

Exercise 1

#include <iostream>

using std::cout;

using std::cin;

int main()

{

int curentyear = 2021, birthyear, age;

cout << “What year were you born in?: “;

cin >> birthyear;

age = curentyear – birthyear;

cout << “This is your age: ” << age;

if (age >= 18)

{

cout << “\nYou are 18 or older!”;

}

else

{

cout << “You are younger than 18!”;

}

return 0;

}

Exercise 2

#include <iostream>

using std::cout;

using std::cin;

int hi_score = 500;

int main()

{

     int score1, score2;

     cout << “Player one please input a number: “;

     cin >> score1;

     cout << “Player two please input a number: “;

     cin >> score2;

     if (score1 >= hi_score)

     {

         cout << “Score 1 is the new high score!:” << score1;

     }

     else

     {

         cout << “\nScore 1 is not the new high score!: “;

     }

     if (score2 >= hi_score)

     {

         cout << “\nScore 2 is the new high score!: ” << score2;

     }

     else

     {

         cout << “\nScore 2 is not the new high score!: “;

     }

     if (score1 > score2)

     {

         cout << “Player 1’s score is higher! \n”;

     }

     else

     {

         cout << “Player 2’s score is higher! \n”;

     }

    if (score1 == score2)

        cout << “Both Players had the same score! \n”;

    return 0;

}

Exercise 3

#include <iostream>

using std::cout;

using std::cin;

int main()

{

int num1, num2, num3, num4, num5;

cout << “Please enter num1: \n”;

cin >> num1;

cout << “Please enter num2: \n”;

cin >> num2;

cout << “Please enter num3: \n”;

cin >> num3;

cout << “Please enter num4: \n”;

cin >> num4;

cout << “Please enter num5: \n”;

cin >> num5;

if (num1 % 2 == 0)

cout << ” number 1 is even : ” << num1 << “\n”;

else

cout << ” number 1 is odd : ” << num1 << “\n”;

if (num2 % 2 == 0)

cout << ” number 2 is even : ” << num2 << “\n”;

else

cout << ” number 2 is odd : ” << num2 << “\n”;

if (num3 % 2 == 0)

cout << ” number 3 is even : ” << num3 << “\n”;

else

cout << ” number 3 is odd : ” << num3 << “\n”;

if (num4 % 2 == 0)

cout << ” number 4 is even : ” << num4 << “\n”;

else

cout << ” number 4 is odd : ” << num4 << “\n”;

if (num5 % 2 == 0)

cout << ” number 5 is even : ” << num5 << “\n”;

else

cout << ” number 5 is odd : ” << num5 << “\n”;

return 0;

}

Exercise 4

#include <iostream>

using std::cout;

using std::cin;

int main()

{

int exam_score;

cout << “Please enter your exam score: \n”;

cin >> exam_score;

if (exam_score <= 39)

cout << “Fail\n”;

else if (exam_score >= 40 && exam_score <= 49)

cout << “Grade : 3rd\n”;

else if (exam_score >= 50 && exam_score <= 59)

cout << “Grade : 2:2 \n”;

else if (exam_score >= 60 && exam_score <= 69)

cout << “Grade : 2:1\n”;

else if (exam_score >= 70 && exam_score <= 100)

cout << “Grade : 1st\n”;

if (exam_score > 100)

cout << “Invalid mark entered”;

return 0;

}

Exercise 5

#include <iostream>

using std::cout;

using std::cin;

int choice = 0;

int main()

{

int month = 0;

cout << “Input a month: 1 = July, 2 = December, Any other = work “;

cin >> month;

if (month == 1)

{

cout << “It is July, I will fly to Egypt! \n”;

int choice;

    cout << “Is there a sandstorm? Type 1 for yes or 2 for no: \n”;

cin >> choice;

if (choice == 1)

cout << “There is a sandstorm! I will stay in and go for a swim.”;

else

cout << “I will go for a camel ride and then see the Pyramids.”;

}

if (month == 2)

{

cout << “It is December, I will go to the Alps! \n”;

int choice;

cout << “Is there snow? Type 1 for yes or 2 for no: \n”;

cin >> choice;

if (choice == 1)

cout << “There is snow! I will go skiing.”;

else

cout << “I will eat fondue!”;

}

{

if (month != (month == 1) || (month == 2) || choice != (choice == 1) || (choice == 2))

cout << “I have work.”;

}

return 0;

}

Exercise 6

#include <iostream>

using std::cout;

using std::cin;

int main()

{

int num1, num2;

cout << “Enter Number 1:”;

cin >> num1;

cout << “Enter Number 2:”;

cin >> num2;

if (num1 >= 10 && num1 <= 20)

cout << “number1 is between 10 and 20\n”;

if (num1 == 1234 || num2 == 1234)

cout << “number1 or number2 is equal to 1234”;

if (num1 > 0 && (num2 >= 12 && num2 <= 32))

cout << “number1 is greater than 0 and number 2 is between 12 and 32\n”;

if (num1 > num2 || num1 < 0)

cout << “number1 is greater than number2 or number1 is negative\n”;

if (num1 < 10 && num1 > 20); // if (num1 <10 || num1 > 20)

    cout << “number1 is not between 10 and 20”;

if ((num1 == 6 || num1 == 9) && (num2 == 2 || 3))

cout << “number1 is equal to 6 or 9 and number2 is equal to 2 or 3”;

if (((num1 == 6 || num1 == 9) && (num2 == 2 || 3)) || num1 > num2)

cout << “number1 is equal to 6 or 9 and number2 is equal to 2 or 3 or number1 is greater than number2”;

return 0;

}

Design a site like this with WordPress.com
Get started