C++ Coding Exercises For Beginners

8 min read Jul 02, 2024
C++ Coding Exercises For Beginners

C++ Coding Exercises for Beginners

This article provides a collection of beginner-friendly C++ coding exercises to help you solidify your understanding of fundamental concepts and build your programming skills. These exercises are designed to be progressively challenging, starting with basic syntax and gradually introducing more complex topics.

Exercise 1: Hello World

Objective: Print "Hello World!" to the console.

Code:

#include 

int main() {
  std::cout << "Hello World!" << std::endl;
  return 0;
}

Explanation:

  • #include <iostream>: This line includes the iostream library, which provides input and output functionality, including the std::cout object.
  • int main() { ... }: This is the main function, the entry point of your program.
  • std::cout << "Hello World!" << std::endl; This line prints the string "Hello World!" to the console and moves the cursor to the next line using std::endl.
  • return 0; This statement indicates successful program execution.

Exercise 2: Simple Calculator

Objective: Write a program that takes two numbers as input from the user, performs a chosen operation (addition, subtraction, multiplication, or division), and displays the result.

Code:

#include 

int main() {
  double num1, num2, result;
  char operation;

  std::cout << "Enter first number: ";
  std::cin >> num1;

  std::cout << "Enter second number: ";
  std::cin >> num2;

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

  switch (operation) {
    case '+':
      result = num1 + num2;
      break;
    case '-':
      result = num1 - num2;
      break;
    case '*':
      result = num1 * num2;
      break;
    case '/':
      if (num2 == 0) {
        std::cout << "Error: Cannot divide by zero!" << std::endl;
        return 1;
      } else {
        result = num1 / num2;
      }
      break;
    default:
      std::cout << "Invalid operation!" << std::endl;
      return 1;
  }

  std::cout << "Result: " << result << std::endl;
  return 0;
}

Explanation:

  • Input: The program prompts the user for two numbers and the desired operation.
  • Switch Statement: The switch statement handles different operations based on the user input.
  • Error Handling: The program checks for division by zero and displays an error message if encountered.
  • Output: The result of the calculation is displayed to the user.

Exercise 3: Swap Two Variables

Objective: Write a program that swaps the values of two variables without using a temporary variable.

Code:

#include 

int main() {
  int num1, num2;

  std::cout << "Enter first number: ";
  std::cin >> num1;

  std::cout << "Enter second number: ";
  std::cin >> num2;

  std::cout << "Before swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;

  num1 = num1 + num2;
  num2 = num1 - num2;
  num1 = num1 - num2;

  std::cout << "After swapping: num1 = " << num1 << ", num2 = " << num2 << std::endl;
  return 0;
}

Explanation:

  • Input: The program takes two integer values as input.
  • Swapping: The code uses arithmetic operations to swap the values without using a temporary variable. The key is to add the two numbers, subtract the second number from the sum (giving the original first number), and subtract the second number from the result (giving the original second number).
  • Output: The program displays the values of the variables before and after the swap.

Exercise 4: Find the Largest of Three Numbers

Objective: Write a program that finds the largest of three numbers entered by the user.

Code:

#include 

int main() {
  int num1, num2, num3, largest;

  std::cout << "Enter first number: ";
  std::cin >> num1;

  std::cout << "Enter second number: ";
  std::cin >> num2;

  std::cout << "Enter third number: ";
  std::cin >> num3;

  largest = num1;
  if (num2 > largest) {
    largest = num2;
  }
  if (num3 > largest) {
    largest = num3;
  }

  std::cout << "The largest number is: " << largest << std::endl;
  return 0;
}

Explanation:

  • Input: The program takes three numbers as input.
  • Finding the Largest: The code initializes a variable largest with the first number and then compares it to the other two numbers. If any number is greater, largest is updated accordingly.
  • Output: The program prints the largest of the three numbers.

Exercise 5: Check Prime Number

Objective: Write a program that determines whether a given number is a prime number.

Code:

#include 

int main() {
  int num, i;
  bool isPrime = true;

  std::cout << "Enter a positive integer: ";
  std::cin >> num;

  if (num <= 1) {
    isPrime = false;
  } else {
    for (i = 2; i * i <= num; i++) {
      if (num % i == 0) {
        isPrime = false;
        break;
      }
    }
  }

  if (isPrime) {
    std::cout << num << " is a prime number." << std::endl;
  } else {
    std::cout << num << " is not a prime number." << std::endl;
  }
  return 0;
}

Explanation:

  • Input: The program takes a positive integer as input.
  • Prime Number Logic:
    • If the number is less than or equal to 1, it is not prime.
    • The code iterates from 2 to the square root of the number. If any number in this range divides the input number evenly, it is not prime.
    • If the loop completes without finding a divisor, the number is prime.
  • Output: The program prints a message indicating whether the number is prime or not.

These exercises cover a range of basic C++ concepts and provide a solid foundation for further learning. As you progress, explore more challenging exercises involving arrays, functions, classes, and other advanced C++ features.

Latest Posts


Featured Posts