Introduction to C++

1. Basic Program

hello.cpp
#include <iostream>
 
using namespace std;
 
int main() {
    cout << "Hello World";
    return 0;
}



2. Explanation of Program

  • #include iostream: Used for input and output operations.

  • using namespace std;: Allows use of standard library objects without writing std::.

  • cout: Used to display output.

  • int main(): Main function where program execution begins.

  • cout: Used to display output.

  • return 0;: Indicates successful execution




3. Applications of C++

FieldUsage
Game DevelopmentGame engines
Operating SystemsWindows, Linux components
Web BrowsersBrowser engines
Embedded SystemsMicrocontrollers
DatabasesMySQL
Competitive ProgrammingFast execution



4. Variables & Data Types

Variables are named memory locations that hold values.


Data types define the kind of data a variable can hold.

Data TypeSizeExample
int4 Bytes10
float4 Bytes3.14
double8 Bytes10.5678
char1 Byte'A'
bool1 Bytetrue
short2 Bytes120
long4 or 8 Bytes123456
long long8 Bytes9876543210
stringDepends on Length"Hello"

datatypes.cpp
#include <iostream>
using namespace std;
 
int main()
{
 
    int num = 534;
    cout << num << endl;
 
    long int lnum = 34;
    cout << lnum << endl;
 
    float x = 35.99;
    cout << x << endl;
 
    double a = 93.98;
    cout << a << endl;
 
    char c = 'A';
    cout << c << endl;
 
    bool isTrue = false;
    cout <<  isTrue << endl;
 
    string str = "Hello World!";
    cout << str << endl;
 
    return 0;
}



5. Type Casting

Type casting is the process of converting one data type to another.

  • Automatic conversion of lower precision to higher precision data type.
  • No risk of data loss.

Example:

implicit-cast.cpp
#include <iostream>
using namespace std;
 
int main()
{
    int a = 10;
    double b = a;
    cout << b << endl;
 
    return 0;
}


  • Manual conversion of higher precision to lower precision data type.
  • Risk of data loss.

Example:

explicit-cast.cpp
#include <iostream>
using namespace std;
 
int main()
{
    double a = 10.5;
 
    int b = (int)a;
    // int b = static_cast<int>(a);
 
    cout << b << endl;
 
    return 0;
}



6. Conditional Statement

Conditional statements are used to make decisions on the basis of a condition.

Write a program to check if a number is even or odd using if-else statement.

even-odd.cpp
#include <iostream> 
using namespace std;
 
int main() {
    int n;
    cout << "Enter a number: ";
 
    cin >> n; //? read the user input
    
    if (n % 2 == 0) {
        cout << n << " is even no." << endl;
    } else {
        cout << n << " is odd no." << endl;
    }
 
    return 0;
}



7. Loop Statement

Loop statements are used to repeat a block of code until a condition condition is met.

  • for loop
  • while loop
  • do-while loop
  • Used to repeat a block of code a fixed number of times.
  • Syntax: for (initialization; condition; increment/decrement)

for-loop.cpp
#include <iostream>
using namespace std;
 
int main()
{
    for (int i = 0; i < 5; i++)
    {
        cout << i << endl;
    }
 
    return 0;
}


  • Used to repeat a block of code until a condition is met.
  • Syntax: while (condition)

while-loop.cpp
#include <iostream>
using namespace std;
 
int main()
{
    int i = 0;
    while (i < 5)
    {
        cout << i << endl;
        i++;
    }
 
    return 0;
}


  • Used to repeat a block of code at least once.
  • Syntax: do while (condition);

do-while-loop.cpp
#include <iostream>
using namespace std;
 
int main()
{
    int i = 0;
    do
    {
        cout << i << endl;
        i++;
    } while (i < 5);
 
    return 0;
}



8. Operators

Operators are symbols that are used to perform operations on variables and values.

  • + (Addition)
  • - (Subtraction)
  • * (Multiplication)
  • / (Division)
  • % (Modulus)


  • = (Assignment)
  • += (Addition Assignment)
  • -= (Subtraction Assignment)
  • *= (Multiplication Assignment)
  • /= (Division Assignment)
  • %= (Modulus Assignment)


  • == (Equal)
  • != (Not Equal)
  • == (Identity)
  • !== (Not Identity)
  • < (Less Than)
  • > (Greater Than)
  • <= (Less Than or Equal)
  • >= (Greater Than or Equal)


  • && (AND)
  • || (OR)
  • ! (NOT)


  • & (AND)
  • | (OR)
  • ~ (NOT)
  • ^ (XOR)
  • << (Left Shift)
  • >> (Right Shift)


  • ++ (Pre-Increment): first increment then use the value
  • -- (Pre-Decrement): first decrement then use the value
  • ++ (Post-Increment): first use the value then increment
  • -- (Post-Decrement): first use the value then decrement


Example:

operators.cpp
#include <iostream>
 
using namespace std;
 
int main() {
    //* Arithmetic Operators
    int a = 5, b = 3;
    cout << "a + b = " << a + b << endl;
    cout << "a - b = " << a - b << endl;
    cout << "a * b = " << a * b << endl;
    cout << "a / b = " << a / b << endl;
    cout << "a % b = " << a % b << endl;
 
    //* Relational Operators
    cout << "a == b = " << (a == b) << endl;
    cout << "a != b = " << (a != b) << endl;
    cout << "a > b = " << (a > b) << endl;
    cout << "a < b = " << (a < b) << endl;
    cout << "a >= b = " << (a >= b) << endl;
    cout << "a <= b = " << (a <= b) << endl;
 
    //* Logical Operators
    cout << "a && b = " << (a && b) << endl;
    cout << "a || b = " << (a || b) << endl;
 
    //* Bitwise Operators
    cout << "a & b = " << (a & b) << endl;
    cout << "a | b = " << (a | b) << endl;
    cout << "~a = " << (~a) << endl;
    cout << "a << b = " << (a << b) << endl;
    cout << "a >> b = " << (a >> b) << endl;
 
    //* Assignment Operators
    cout << "a += b = " << (a += b) << endl;
    cout << "a -= b = " << (a -= b) << endl;
    cout << "a *= b = " << (a *= b) << endl;
    cout << "a /= b = " << (a /= b) << endl;
    cout << "a %= b = " << (a %= b) << endl;
 
    return 0;
}



9. Switch Statement, Break & Continue

  • Used to execute one of many blocks of code based on the value of a variable.
  • Syntax: switch (expression) { case label: code; break; default: code; break; }

switch.cpp
#include <iostream>
using namespace std;
 
int main()
{
    int a = 3;
    switch (a)
    {
    case 1:
        cout << "a is 1" << endl;
        break;
    case 2:
        cout << "a is 2" << endl;
        break;
    case 3:
        cout << "a is 3" << endl;
        break;
    default:
        cout << "a is not 1, 2, or 3" << endl;
        break;
    }
    return 0;
}


  • Used to exit a loop or switch statement.
  • Syntax: break;


  • Used to skip the current iteration of a loop and continue with the next iteration.
  • Syntax: continue;



10. Some Basic Programs

Write a program to print the multiplication table of n.

Code:

multiplication.cpp
#include <iostream>
using namespace std;
 
int main()
{
    int n;
    cout << "Enter value of n: ";
    cin >> n;
 
    for (int i = 1; i <= 10; i++)
    {
        cout << n << " * " << i << " = " << n * i << endl;
    }
 
    return 0;
}

Output:

Enter value of n: 12 12 * 1 = 12 12 * 2 = 24 12 * 3 = 36 12 * 4 = 48 12 * 5 = 60 12 * 6 = 72 12 * 7 = 84 12 * 8 = 96 12 * 9 = 108 12 * 10 = 120



Write a program to calculate the power of a number.

Code:

power.cpp
#include <iostream>
using namespace std;
 
int main()
{
    int base, power;
    cout << "Enter a base: ";
    cin >> base;
    cout << "Enter a power: ";
    cin >> power;
 
    int res = base;
 
    for (int i = 1; i < power; i++)
    {
        res = res * base;
    }
    cout << base << "^" << power << " = " << res << endl;
 
    return 0;
}

Output:

Enter a base: 3 Enter a power: 3 3^3 = 27



Write a program to check if a number is prime or not.

Code:

prime.cpp
#include <iostream>
using namespace std;
 
int main()
{
    int n;
    cout << "Enter a no: ";
    cin >> n;
    bool isPrime = false;
 
    if (n < 2)
    {
        isPrime = false;
    }
    else if (n == 2)
    {
        isPrime = true;
    }
    else if (n % 2 == 0)
    {
        isPrime = false;
    }
    else
    {
        for (int i = 3; i * i <= n; i+=2)
        {
            if (n % i == 0)
            {
                isPrime = false;
                break;
            }
        }
        isPrime = true;
    }
 
    if (isPrime)
    {
        cout << n << " is a prime no." << endl;
    }
    else
    {
        cout << n << " is not a prime no." << endl;
    }
 
    return 0;
}

Output:

Enter a no: 12 12 is not a prime no.



Write a program to print the first n Fibonacci numbers.

n = 11: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,

Code:

fibonacci.cpp
#include <iostream>
 
using namespace std;
 
int main()
{
    int n;
 
    cout << "Enter a no: ";
    cin >> n;
 
    int a = 0, b = 1, c = 0;
 
    for (int i = 1; i <= n; i++)
    {
        cout << c << ", ";
        a = b;
        b = c;
        c = a + b;
    }
    
    return 0;
}

Output:

Enter a no: 12 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,