codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

12 / 87 topics
6Keywords and Identifiers7Variables, Literals, Constants & Storage Classes8Data Types & Type Modifiers9Type Conversion & Casting Operators10Operators11Booleans12Math & Numbers13Date and Time
Tutorials/C++ Programming/Math & Numbers
⚡C++ Programming

Math & Numbers

Updated 2026-04-20
3 min read

Math & Numbers

In this section, we will explore how to work with numbers and perform mathematical operations in C++. Understanding these concepts is crucial for any programmer as they form the foundation of numerical computations in software development.

Basic Data Types for Numbers

C++ provides several data types to handle different ranges and precision requirements for numeric values. Here are some commonly used numeric data types:

  • int: Represents integer values.
  • float: Represents single-precision floating-point numbers.
  • double: Represents double-precision floating-point numbers.
  • long: Represents long integers, typically larger than int.
  • short: Represents short integers, typically smaller than int.

Example: Declaring and Initializing Numeric Variables

#include <iostream>

int main() {
    int age = 25;
    float height = 5.9f; // Note the 'f' suffix for floats
    double salary = 75000.50;
    long population = 1000000L; // Note the 'L' suffix for longs
    short temperature = -10;

    std::cout << "Age: " << age << std::endl;
    std::cout << "Height: " << height << std::endl;
    std::cout << "Salary: " << salary << std::endl;
    std::cout << "Population: " << population << std::endl;
    std::cout << "Temperature: " << temperature << std::endl;

    return 0;
}

Best Practices

  • Use appropriate data types: Choose the smallest data type that can hold your values to save memory.
  • Suffixes for literals: Use f for floats and L for longs to avoid implicit conversions.

Arithmetic Operations

C++ supports basic arithmetic operations such as addition, subtraction, multiplication, division, and modulus. These operations are performed using the following operators:

  • +: Addition
  • -: Subtraction
  • *: Multiplication
  • /: Division
  • %: Modulus (remainder of division)

Example: Performing Arithmetic Operations

#include <iostream>

int main() {
    int a = 10;
    int b = 3;

    std::cout << "Addition: " << a + b << std::endl; // Output: 13
    std::cout << "Subtraction: " << a - b << std::endl; // Output: 7
    std::cout << "Multiplication: " << a * b << std::endl; // Output: 30
    std::cout << "Division: " << a / b << std::endl; // Output: 3 (integer division)
    std::cout << "Modulus: " << a % b << std::endl; // Output: 1

    return 0;
}

Important Notes

  • Integer Division: When dividing two integers, the result is also an integer. Any fractional part is discarded.
  • Floating-point Division: If at least one operand is a floating-point number, the division results in a floating-point number.

Increment and Decrement Operators

C++ provides increment (++) and decrement (--) operators to increase or decrease the value of a variable by 1. These operators can be used as prefix or postfix.

Example: Using Increment and Decrement Operators

#include <iostream>

int main() {
    int count = 5;

    std::cout << "Initial Count: " << count << std::endl; // Output: 5
    std::cout << "Prefix Increment: " << ++count << std::endl; // Output: 6
    std::cout << "Postfix Increment: " << count++ << std::endl; // Output: 6
    std::cout << "After Postfix Increment: " << count << std::endl; // Output: 7

    return 0;
}

Best Practices

  • Use prefix form when possible: It is more efficient and can make your code clearer.
  • Avoid using increment/decrement in complex expressions: It can lead to unexpected results.

Mathematical Functions

C++ provides a set of mathematical functions through the <cmath> library. These functions include trigonometric, logarithmic, exponential, and rounding operations.

Example: Using Mathematical Functions

#include <iostream>
#include <cmath>

int main() {
    double angle = 45.0;
    double radians = angle * M_PI / 180.0; // Convert degrees to radians

    std::cout << "Sine of " << angle << " degrees: " << sin(radians) << std::endl;
    std::cout << "Cosine of " << angle << " degrees: " << cos(radians) << std::endl;
    std::cout << "Exponential of 2: " << exp(2.0) << std::endl;
    std::cout << "Logarithm base 10 of 100: " << log10(100.0) << std::endl;

    return 0;
}

Best Practices

  • Include <cmath>: Always include this header to use mathematical functions.
  • Use constants like M_PI: For better readability and accuracy.

Random Number Generation

Generating random numbers is often required in simulations, games, and other applications. C++ provides facilities for generating pseudo-random numbers through the <cstdlib> and <ctime> libraries.

Example: Generating Random Numbers

#include <iostream>
#include <cstdlib> // For rand() and srand()
#include <ctime>   // For time()

int main() {
    // Seed the random number generator with the current time
    std::srand(static_cast<unsigned int>(std::time(nullptr)));

    // Generate a random number between 1 and 100
    int randomNumber = std::rand() % 100 + 1;

    std::cout << "Random Number: " << randomNumber << std::endl;

    return 0;
}

Best Practices

  • Seed the generator: Use std::srand with a seed like the current time to ensure different sequences of random numbers.
  • Range control: Use modulo operation to limit the range of generated numbers.

Conclusion

In this section, we have covered the basics of working with numbers and performing mathematical operations in C++. Understanding these fundamental concepts is essential for any programmer. By mastering these topics, you will be well-equipped to handle numerical computations in your C++ programs.


PreviousBooleansNext Date and Time

Recommended Gear

BooleansDate and Time