C++ Beginner Tasks: Code Solutions & Explanations

by Admin 50 views
C++ Beginner Tasks: Code Solutions & Explanations

Hey everyone! 👋 Let's dive into some beginner-friendly C++ tasks! We'll break down the code, explain what's happening, and make sure you understand the core concepts. These tasks are designed to get you comfortable with the basics of C++ programming. We will go through the solutions one by one, covering a range of topics from basic input/output to simple calculations and manipulations. Get ready to learn, code, and have some fun! 🚀

Task 1: Greeting the User 🙋‍♀️

This is a super simple program to get you started. It asks for your name and then greets you! Let's check out the code and see how it works:

#include <iostream>
using namespace std;

int main() {
    string ad;
    cout<< "Adınızı daxil edin"; // Prompt the user for their name
    cin>>ad; // Read the user's input and store it in the 'ad' variable
    cout<<"Salam,"<<ad<<"!"<<endl; // Output a greeting to the console
    return 0;
}

Explanation:

  • #include <iostream>: This line includes the iostream library, which provides the tools for input and output operations (like displaying text on the screen and getting input from the keyboard). Think of it as importing the necessary tools.
  • using namespace std;: This line tells the compiler that we'll be using elements from the std namespace (like cout, cin, and endl). This simplifies the code by avoiding the need to write std:: before each of these elements.
  • string ad;: This declares a variable named ad of type string. A string is a sequence of characters, like your name. We're using it to store the user's name.
  • cout << "Adınızı daxil edin";: This line uses cout (an output stream) to display the message "Adınızı daxil edin" (Enter your name) on the console. It's like the program asking the user for their name.
  • cin >> ad;: This line uses cin (an input stream) to read the user's input from the console and store it in the ad variable. The program waits here until the user types something and presses Enter.
  • cout << "Salam," << ad << "!" << endl;: This line uses cout to display the greeting "Salam," followed by the user's name (stored in the ad variable), and an exclamation mark. endl inserts a newline character, moving the cursor to the next line. Voila! The program greets the user! This is one of the easiest introductory tasks out there.

Task 2: Arithmetic Operations ➕➖✖️➗

In this task, we will perform some basic arithmetic operations - addition, subtraction, multiplication, and division! Here is the code:

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cout << "İki tam ədəd daxil edin: "; // Prompt the user to enter two integers
    cin >> a >> b; // Read two integers from the user

    cout << "Cəmi: " << a + b << endl; // Calculate and display the sum
    cout << "Fərqi: " << a - b << endl; // Calculate and display the difference
    cout << "Hasil: " << a * b << endl; // Calculate and display the product

    if (b != 0)
        cout << "Bölmə: " << (float)a / b << endl; // Calculate and display the quotient if b is not zero
    else
        cout << "0-a bölmək mümkün deyil!" << endl; // Display an error message if b is zero

    return 0;
}

Explanation:

  • int a, b;: This declares two integer variables, a and b, to store the numbers entered by the user.
  • cout << "İki tam ədəd daxil edin: ";: This prompts the user to enter two integers.
  • cin >> a >> b;: This reads two integers from the user and stores them in the variables a and b.
  • cout << "Cəmi: " << a + b << endl;: This calculates the sum of a and b and displays it on the console.
  • cout << "Fərqi: " << a - b << endl;: This calculates the difference between a and b and displays it.
  • cout << "Hasil: " << a * b << endl;: This calculates the product of a and b and displays it.
  • if (b != 0): This checks if the value of b is not equal to zero to avoid division by zero errors.
  • cout << "Bölmə: " << (float)a / b << endl;: If b is not zero, this line calculates the quotient of a divided by b. We cast a to a float to ensure that the result is a floating-point number (e.g., 2.5 instead of 2). This prevents the loss of information.
  • else cout << "0-a bölmək mümkün deyil!" << endl;: If b is zero, this line displays an error message indicating that division by zero is not possible. This is very important for proper execution. Always think about edge cases like these.

Task 3: Squaring and Cubing Numbers 📐

This program takes a number as input and calculates its square and cube. Let's look at the code:

#include <iostream>
using namespace std;

int main() {
    double eded;
    cout << "Bir ədəd daxil edin: "; // Prompt the user to enter a number
    cin >> eded; // Read a number from the user

    cout << "Ədədin kvadratı: " << eded * eded << endl; // Calculate and display the square
    cout << "Ədədin kubu: " << eded * eded * eded << endl; // Calculate and display the cube

    return 0;
}

Explanation:

  • double eded;: This declares a variable named eded of type double. double is used to store floating-point numbers (numbers with decimal points).
  • cout << "Bir ədəd daxil edin: ";: This prompts the user to enter a number.
  • cin >> eded;: This reads the number entered by the user and stores it in the eded variable.
  • cout << "Ədədin kvadratı: " << eded * eded << endl;: This calculates the square of eded (eded multiplied by itself) and displays it.
  • cout << "Ədədin kubu: " << eded * eded * eded << endl;: This calculates the cube of eded (eded multiplied by itself three times) and displays it. Simple, right?

Task 4: Circle Calculations ⭕

This task involves calculating the area and circumference of a circle. Here is the program:

#include <iostream>
using namespace std;

int main() {
    const double PI = 3.14159; // Define a constant for PI
    double r;

    cout << "Dairənin radiusunu daxil edin: "; // Prompt the user to enter the radius
    cin >> r; // Read the radius from the user

    double sahe = PI * r * r; // Calculate the area of the circle
    double cevre = 2 * PI * r; // Calculate the circumference of the circle

    cout << "Dairənin sahəsi: " << sahe << endl; // Display the area
    cout << "Dairənin çevrəsi: " << cevre << endl; // Display the circumference

    return 0;
}

Explanation:

  • const double PI = 3.14159;: This line declares a constant variable PI and initializes it with the value of pi. Using const means that the value of PI cannot be changed during the program's execution. It's good practice to use constants for values that shouldn't be altered.
  • double r;: This declares a variable named r of type double to store the radius of the circle.
  • cout << "Dairənin radiusunu daxil edin: ";: This prompts the user to enter the radius of the circle.
  • cin >> r;: This reads the radius entered by the user and stores it in the r variable.
  • double sahe = PI * r * r;: This calculates the area of the circle using the formula π * r^2 and stores the result in the sahe variable.
  • double cevre = 2 * PI * r;: This calculates the circumference of the circle using the formula 2 * π * r and stores the result in the cevre variable.
  • cout << "Dairənin sahəsi: " << sahe << endl;: This displays the calculated area of the circle.
  • cout << "Dairənin çevrəsi: " << cevre << endl;: This displays the calculated circumference of the circle. This is a very essential geometry problem!

Task 6: Average of Three Numbers 🧮

This program calculates the average of three numbers entered by the user. Here's the code:

#include <iostream>
using namespace std;

int main() {
    double a, b, c;

    cout << "Üç ədəd daxil edin: "; // Prompt the user to enter three numbers
    cin >> a >> b >> c; // Read three numbers from the user

    double ortalama = (a + b + c) / 3; // Calculate the average

    cout << "Ədədərin ortalaması: " << ortalama << endl; // Display the average

    return 0;
}

Explanation:

  • double a, b, c;: This declares three variables, a, b, and c, of type double to store the three numbers entered by the user.
  • cout << "Üç ədəd daxil edin: ";: This prompts the user to enter three numbers.
  • cin >> a >> b >> c;: This reads three numbers from the user and stores them in the variables a, b, and c.
  • double ortalama = (a + b + c) / 3;: This calculates the average of the three numbers by summing them and dividing the result by 3.
  • cout << "Ədədərin ortalaması: " << ortalama << endl;: This displays the calculated average. Not too hard, right?

Task 7: Swapping Variables 🔄

This program swaps the values of two variables. Here is how it's done:

#include <iostream>
using namespace std;

int main() {
    int a, b, temp;
    cout << "Iki eded daxil et: "; // Prompt the user to enter two numbers
    cin >> a >> b; // Read two numbers from the user

    temp = a; // Store the value of 'a' in a temporary variable
    a = b; // Assign the value of 'b' to 'a'
    b = temp; // Assign the value of the temporary variable (original 'a') to 'b'

    cout << "Ededlerin yerləri deyisdirildi:\n"; // Display a message indicating the swap
    cout << "a = " << a << "\n"; // Display the new value of 'a'
    cout << "b = " << b << "\n"; // Display the new value of 'b'

    return 0;
}

Explanation:

  • int a, b, temp;: This declares three integer variables: a and b to store the two numbers, and temp to temporarily hold the value of one of the variables during the swap.
  • cout << "Iki eded daxil et: ";: This prompts the user to enter two numbers.
  • cin >> a >> b;: This reads two numbers from the user and stores them in the variables a and b.
  • temp = a;: This assigns the value of a to the temp variable. This is crucial; we are backing up the value of a.
  • a = b;: This assigns the value of b to a. Now, a has the original value of b.
  • b = temp;: This assigns the value stored in temp (the original value of a) to b. Now, b has the original value of a, and the swap is complete.
  • The rest of the code is for outputting the swapped values. Swapping variables is a fundamental concept in programming, useful in many different contexts.

Task 8: Square Calculations 🟦

This task involves calculating the area and perimeter of a square. Let's see the code:

#include <iostream>
using namespace std;

int main() {
    double t;
    cout << "Kvadratın tərəfini daxil edin: "; // Prompt the user to enter the side of the square
    cin >> t; // Read the side from the user

    double sahə = t * t; // Calculate the area of the square
    double perimetr = 4 * t; // Calculate the perimeter of the square

    cout << "Kvadratın sahəsi: " << sahə << endl; // Display the area
    cout << "Kvadratın perimetri: " << perimetr << endl; // Display the perimeter

    return 0;
}

Explanation:

  • double t;: This declares a variable named t of type double to store the length of the side of the square.
  • cout << "Kvadratın tərəfini daxil edin: ";: This prompts the user to enter the side length.
  • cin >> t;: This reads the side length entered by the user and stores it in the t variable.
  • double sahə = t * t;: This calculates the area of the square by multiplying the side length by itself (t * t).
  • double perimetr = 4 * t;: This calculates the perimeter of the square by multiplying the side length by 4 (since a square has four equal sides).
  • cout << "Kvadratın sahəsi: " << sahə << endl;: This displays the calculated area of the square.
  • cout << "Kvadratın perimetri: " << perimetr << endl;: This displays the calculated perimeter of the square. A very simple and effective geometric problem! Always think about how the code works and the underlying formulas.

Task 9: Time Conversion ⏱️

This program converts seconds into hours, minutes, and seconds. Let's look at it:

#include <iostream>
using namespace std;

int main() {
    int saniyə;
    cout << "Saniyeni daxil edin: "; // Prompt the user to enter seconds
    cin >> saniyə; // Read the number of seconds from the user

    int saat = saniyə / 3600; // Calculate the number of hours
    int dəqiqə = (saniyə % 3600) / 60; // Calculate the number of minutes
    int qalan_saniyə = saniyə % 60; // Calculate the remaining seconds

    cout << "Saat: " << saat << endl; // Display the hours
    cout << "Dəqiqə: " << dəqiqə << endl; // Display the minutes
    cout << "Saniyə: " << qalan_saniyə << endl; // Display the remaining seconds

    return 0;
}

Explanation:

  • int saniyə;: This declares an integer variable named saniyə to store the number of seconds entered by the user.
  • cout << "Saniyeni daxil edin: ";: This prompts the user to enter the number of seconds.
  • cin >> saniyə;: This reads the number of seconds entered by the user and stores it in the saniyə variable.
  • int saat = saniyə / 3600;: This calculates the number of hours by dividing the total seconds by 3600 (the number of seconds in an hour). Integer division is used, so any fractional part is discarded.
  • int dəqiqə = (saniyə % 3600) / 60;: This calculates the number of minutes. First, saniyə % 3600 calculates the remainder after dividing the total seconds by 3600 (i.e., the remaining seconds after removing the hours). Then, this remainder is divided by 60 to get the number of minutes.
  • int qalan_saniyə = saniyə % 60;: This calculates the remaining seconds by taking the remainder of the total seconds divided by 60.
  • The rest of the code is for outputting the calculated hours, minutes, and remaining seconds. Time conversion is a very common task!

I hope you enjoyed these C++ beginner tasks! Keep practicing, and you'll become a coding pro in no time! Keep coding, and don't be afraid to experiment! 🎉