C++ Basic Programs Examples

7 min read Jul 01, 2024
C++ Basic Programs Examples

C++ Basic Programs Examples

This article will guide you through several basic C++ programs, illustrating fundamental concepts and providing a foundation for your journey in C++ programming.

1. Hello World!

The classic starting point for any programming language. This program simply displays "Hello World!" on the screen.

#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 functionalities like std::cout for printing to the console.
  • int main(): This is the main function, the program's starting point.
  • std::cout << "Hello World!" << std::endl;: This line prints the string "Hello World!" to the console. std::endl inserts a newline character, moving the cursor to the next line.
  • return 0;: This line indicates successful program execution.

2. Adding Two Numbers

This program takes two numbers as input from the user, adds them, and then displays the result.

#include 

int main() {
  int num1, num2, sum;

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

  sum = num1 + num2;

  std::cout << "Sum of the two numbers is: " << sum << std::endl;
  return 0;
}

Explanation:

  • int num1, num2, sum;: Declares three integer variables to store the input numbers and their sum.
  • std::cin >> num1 >> num2;: Reads two numbers from the user's input and stores them in num1 and num2.
  • sum = num1 + num2;: Calculates the sum of the two numbers.
  • std::cout << "Sum of the two numbers is: " << sum << std::endl;: Prints the result to the console.

3. Finding the Largest Number

This program takes three numbers from the user and determines the largest among them.

#include 

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

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

  largest = num1; // Assume the first number is the largest initially

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

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

Explanation:

  • largest = num1;: Initially assumes the first number (num1) is the largest.
  • if (num2 > largest) ...: If the second number (num2) is greater than the current largest, it becomes the new largest.
  • if (num3 > largest) ...: Same logic applied for the third number (num3).

4. Factorial Calculation

This program calculates the factorial of a given number using a loop.

#include 

int main() {
  int num, factorial = 1;

  std::cout << "Enter a number: ";
  std::cin >> num;

  for (int i = 1; i <= num; i++) {
    factorial *= i; 
  }

  std::cout << "Factorial of " << num << " is: " << factorial << std::endl;
  return 0;
}

Explanation:

  • int num, factorial = 1;: Declares variables to store the input number and its factorial.
  • for (int i = 1; i <= num; i++) { ... }: Iterates from 1 to the input number (num).
  • factorial *= i;: Multiplies the current factorial value with the loop counter (i) in each iteration.

5. Checking Prime Number

This program determines if a given number is prime or not.

#include 
#include  // For sqrt() function

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

  std::cout << "Enter a number: ";
  std::cin >> num;

  if (num <= 1) {
    isPrime = false; 
  } else {
    for (int i = 2; i <= sqrt(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:

  • bool isPrime = true;: Initializes a boolean variable to assume the number is prime by default.
  • if (num <= 1) ...: Handles cases where the input is less than or equal to 1 (not prime).
  • for (int i = 2; i <= sqrt(num); i++) { ... }: Iterates from 2 up to the square root of the input number.
  • if (num % i == 0) ...: If the number is divisible by any number in the loop, it's not prime, and isPrime is set to false.
  • if (isPrime) ...: Prints the result based on the isPrime value.

These basic programs demonstrate essential C++ concepts like data types, variables, input/output, control flow (if-else, for loop), and arithmetic operations. They serve as a solid foundation for building more complex C++ programs.

Latest Posts