Skip to main content

Math Basics

Introduction to Basic Math

Before diving into the world of programming, it's essential to build a strong foundation in mathematics, In this section, we will walk through fundamental mathematical concepts in a structured manner, ensuring that you not only understand them but also see how they apply to real-world programming challenges.
Hold onto your excitement and get ready to dive into the world of numbers!

Let's start with a simple question—what is a number?

A number is a symbol that represents a quantity, made up of one or more digits arranged in a specific order. The base of numeral system decides how a number is understood.

For example, in our everyday system(base 10, also called as decimal system), we use digits to form a number, however computer works differently—they use a system called as binary numbers(base 2) to store data which consists of '0' and '1'.

How does a computer store data?

Computer only understand '0' and '1', because they operate only on electrical signals. Every information which is processed by computer is stored in binary number format.

What is '0' and '1' signifies?
0 and 1 represent the two states of a switch—'0' means OFF, and '1' means ON. These on-off states form the foundation of how computers process and store data.

In the standard base-10 (decimal) system, which is commonly used contains ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.

We will learn about base-2, base-8, and base-16 later. As of now, we will focus on base-10 which we also use in day-to-day life while counting.

Lets see how numbers are formed, Using the base-10 system means that each digit in a number is determined by its position and the power of 10.

A decimal consists of a whole number part and then followed by a decimal point (.) and then a fractional part. Each digit after the decimal point represents a fraction where the denominator is a power of 10.

For example, in 12.34:

  • 12 is the whole number part.
  • 3 is in the tenths place → 3×1/10=0.3
  • 4 is in the hundredths place → 4×1/100=0.04

So, 12.34 = 12 + 0.3 + 0.04


As we now have a basic understanding of decimal numbers, let's move on to see how mathematical operations occur in programming. In programming, we use operators like (+, -, /, *, %) to perform basic arithmetic operations, allowing us to manipulate numbers efficiently for calculations, logic building, and data processing. Moving forward, we will explore each of these operators one by one and understand their functionality. Additionally, in the upcoming sections, we will learn how to perform operations between two decimal numbers in programming.

We can use operators like (+, -, /, *, %) to perform these basic operations. Let’s see how we can use these operators one by one.

Addition

If we want to add 2 numbers we can use the operator ‘+’.

Before performing the addition operation lets know a little bit about data types, and there storage limit.

A data type defines the type of value a variable can store, how much memory it occupies, and the operations that can be performed on it. Different programming languages support various data types, and their size may vary depending on the language and system architecture. Below are some common data types used in programming languages like C, C++, and Java:

  • int (4 bytes) → Range: -2,147,483,648 to 2,147,483,647
  • long long (8 bytes) → Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  • float (4 bytes) & double (8 bytes) → Used for decimal numbers but may cause precision loss.
  • long (4 or 8 bytes, depending on language) → Can store larger values than int.

When two numbers are added in programming then the sum of it is calculated on the basis of their data type for example if both the numbers is integer then the resultant value will also be integer Similarly, if they are long, the result will be long, however if the sum exceeds the maximum limit then it leads to integer overflow.

What is Overflow in Addition?

In programming, an overflow occurs when the resultant value, after performing any operation between two numbers, exceeds the storage limit of the data type of the resultant value.

For example, in C++ or Java:

int a = 2147483640; // Close to the max int limit
int b = 10;
int sum = a + b; // Overflow occurs, incorrect result

Since the maximum value for a 32-bit int is 2,147,483,647, adding 10 causes it to wrap around to the negative range, leading to incorrect results.

So be careful while adding 2 numbers. Adding them might cross the integer limits.

Example

Input : num1 = 2, num2 = 5.
Output: 7

Addition Code
C++ Code
#include <bits/stdc++.h>
using namespace std;


int main(){
    int num1=5,num2=2;

    // performing addition
    int ans = num1 + num2;

     // printing result
    cout << ans << endl;
    // output 7
    return 0;
}

Java Code
public class Main {
    public static void main(String[] args) {
        int num1 = 5, num2 = 2;

        // performing addition
        int ans = num1 + num2;

        // printing result
        System.out.println(ans);
        // output 7
    }
}

Python Code
# defining variables
num1 = 5
num2 = 2

# performing addition
ans = num1 + num2

# printing result
print(ans)
# output 7

Javascript Code
// defining variables
let num1 = 5, num2 = 2;

// performing addition
let ans = num1 + num2;

// printing result
console.log(ans);
// output 7

Time Complexity : O(1)

Arithmetic addition between two integers takes constant time.

Space Complexity : O(1)

The operation does not require extra space beyond storing the result.

Subtraction

If we want to subtract 2 numbers(subtract num2 from num1) we can use the operator ‘-’.

The subtraction of 2 integers will give an integer. But as we know integers in programming have a limit because only a certain memory (4Bytes in the case of C++ and Java) is given to them. They can't reach negative infinity.

So be careful while subtracting 2 integers. Subtracting them might cross the integer limits and cause integer underflow.

What is underflow in subtraction?

Integer underflow happens when subtracting two numbers gives a result smaller than the lowest value a data type can store. This makes the number wrap around and turn into a very large value instead of the expected negative number.

int a = -2147483640; // Close to the minimum int limit
int b = 10;
int result = a - b; // Underflow occurs, incorrect result

Subtracting 10 from -2,147,483,640 gives -2,147,483,650, which is smaller than the minimum allowed value for a 32-bit int (-2,147,483,648). Since this exceeds the lower limit, it causes integer underflow.

Example

Input : num1 = 5 , num2 = 2.
Output : 3

Subtraction Code
C++ Code
#include <bits/stdc++.h>
using namespace std;


int main(){
    int num1=5,num2=2;

    // performing subtraction
    int ans = num1 - num2;

     // printing result
    cout << ans << endl;
    // output 3
    return 0;
}

Java Code
public class Main {
    public static void main(String[] args) {
        int num1 = 5, num2 = 2;

        // performing subtraction
        int ans = num1 - num2;

        // printing result
        System.out.println(ans);
        // output 3
    }
}

Python Code
# defining variables
num1 = 5
num2 = 2

# performing subtraction
ans = num1 - num2

# printing result
print(ans)
# output 3

Javascript Code
// defining variables
let num1 = 5, num2 = 2;

// performing subtraction
let ans = num1 - num2;

// printing result
console.log(ans);
// output 3

Time Complexity : O(1)

Arithmetic subtraction between two integers takes constant time.

Space Complexity : O(1)

The operation does not require extra space beyond storing the result.

Multiplication

If we want to multiply 2 numbers we can use the operator ‘*’.

The multiplication of 2 integers will give an integer.

Multiplying them might cross the integer limits and can cause Integer overflow and some times it can also cause Integer underflow.

Underflow and Overflow in multiplication?

Overflow happens when the result of a multiplication exceeds the maximum value that the data type can store, causing it to wrap around and give an incorrect result.

Underflow can occur when multiplying with a negative number, especially in cases where the result falls below the minimum limit of the integer type. This can also lead to unexpected behavior.

Example for integer Overflow
int a = 46341; // A number such that its square will overflow
int b = 46341;
int result = a * b; // Overflow occurs, incorrect result

Since the expected result exceeds 2,147,483,647 (max int value), overflow occurs, producing an incorrect value.

Example of Integer Underflow
int a = -2147483647; // Close to the min limit
int b = 2;
int result = a * b; // Underflow occurs, incorrect result

Since -2147483647 * 2 results in a number smaller than -2,147,483,648, it wraps around, causing underflow.

Example

Input : num1 = 5, num2 = 2.
Output: 10

Multiplication Code
C++ Code
#include <bits/stdc++.h>
using namespace std;


int main(){
    int num1=5,num2=2;

    // performing multiplication
    int ans = num1 * num2;

     // printing result
    cout << ans << endl;
    // output 10
    return 0;
}

Java Code
public class Main {
    public static void main(String[] args) {
        int num1 = 5, num2 = 2;

        // performing multiplication
        int ans = num1 * num2;

        // printing result
        System.out.println(ans);
        // output 10
    }
}

Python Code
# defining variables
num1 = 5
num2 = 2

# performing multiplication
ans = num1 * num2

# printing result
print(ans)
# output 10

Javascript Code
// defining variables
let num1 = 5, num2 = 2;

// performing multiplication
let ans = num1 * num2;

// printing result
console.log(ans);
// output 10

Time Complexity : O(1)

O(1) for modern CPUs, as they are optimized to handle fixed-size integer multiplication in constant time.

Space Complexity : O(1)

The operation does not require extra space beyond storing the result.

Division: Quotient, Remainder

If we want to divide two numbers(num1 is divided by num2), we can simply use the operator ‘/’.

Firstly I would insist you to think by your own for couple of minutes if divison can also cause overflow or underflow.

If you have an answer in mind, great! If not, don’t worry—you’ll understand it clearly by the end of this article.

In division, the numerator is the number being divided (on top), and the denominator is the number by which we divide (on bottom). The result of the division is called the quotient.

int numerator = 10;
int denominator = 2;
int quotient = numerator / denominator; // Quotient = 5

Overflow in division

In division the overflow happens when two numbers are divided and the resultant value exceeds the maximum limit of the resultant data type.

Example of Overflow in int
int a = INT_MIN; // -2,147,483,648 (minimum int value)
int b = -1;
int result = a / b; // Overflow occurs

The expected result of -2,147,483,648 ÷ -1 is 2,147,483,648
But since the max int value is only 2,147,483,647, the result exceeds the limit, causing overflow.

Example : 

Input : num1 = 6, num2 = 2.
Output : 3
Explanation:

Division Code
C++ Code
#include <bits/stdc++.h>
using namespace std;


int main(){
    int num1 = 6,num2 = 2;

    // performing division
    int ans = num1 / num2;

     // printing result
    cout << ans << endl;
    // output 3
    return 0;
}

Java Code
public class Main {
    public static void main(String[] args) {
        int num1 = 6, num2 = 2;

        // performing division
        int ans = num1 / num2;

        // printing result
        System.out.println(ans);
        // output 3
    }
}

Python Code
# defining variables
num1 = 6
num2 = 2

# performing division
ans = num1 // num2  # Integer division

# printing result
print(ans)
# output 3

Javascript Code
// defining variables
let num1 = 6, num2 = 2;

// performing division
let ans = Math.floor(num1 / num2); // Ensures integer result like in C++

/* or simply:
   let ans = num1 / num2; // JavaScript inherently supports floating-point division,
   but for strict integer behavior, use Math.floor if necessary.
*/

// printing result
console.log(ans);
// output 3

Time Complexity : O(1)

Integer division is a basic arithmetic operation performed in constant time on most modern architectures.

Space Complexity : O(1)

The operation does not require extra space beyond storing the result.

Modulo

If we want to perform a modulo operation between two numbers (num1 modulo num2), we can use the modulo operator %. The modulo operation returns the remainder when num1 is divided by num2.

While doing modulo also need to be careful about the overflow.

Overflow in Modulo

Modulo (%) usually doesn’t cause overflow, but an exception occurs when using INT_MIN % -1 in signed integers.

Example of Modulo Overflow:
int a = INT_MIN; // -2,147,483,648 (Minimum int value)
int b = -1;
int result = a % b; // Undefined behavior

The expected result of INT_MIN % -1 is zero, but internally, INT_MIN / -1 exceeds the maximum int value (INT_MAX), leading to undefined behavior.

Example

Input : num1 = 7, num2 = 3.
Output: 1
Explanation: When 7 is divided by 3, the quotient is 2 and the remainder is 1. The modulo operation returns this remainder (7 % 3 = 1).

Code to Find Remainder
C++ Code
#include <bits/stdc++.h>
using namespace std;

int main() {
    int num1 = 7, num2 = 3;

    // performing modulo operation
    int ans = num1 % num2;

    // printing result
    cout << ans << endl;
    // output 1
    return 0;
}

Java Code
public class Main {
    public static void main(String[] args) {
        int num1 = 7, num2 = 3;

        // performing modulo operation
        int ans = num1 % num2;

        // printing result
        System.out.println(ans);
        // output 1
    }
}

Python Code
# defining variables
num1 = 7
num2 = 3

# performing modulo operation
ans = num1 % num2

# printing result
print(ans)
# output 1

Javascript Code
// defining variables
let num1 = 7, num2 = 3;

// performing modulo operation
let ans = num1 % num2;

// printing result
console.log(ans);
// output 1

Time Complexity : O(1)

Modulo is closely related to division and is also a constant-time operation on most modern CPUs.

Space Complexity : O(1)

The operation does not require extra space beyond storing the result.

When 1 is divided by 5 the remainder is 1, so 1 % 5 = 1.

When 2 is divided by 5 the remainder is 2, so 2 % 5 = 2.

When 3 is divided by 5 the remainder is 3, so 3 % 5 = 3.

When 4 is divided by 5 the remainder is 4, so 4 % 5 = 4.

When 5 is divided by 5 the remainder is 0, so 5 % 5 = 0.

When 6 is divided by 5 the remainder is 1, so 6 % 5 = 1.

When 7 is divided by 5 the remainder is 2, so 7 % 5 = 2.

When 8 is divided by 5 the remainder is 3, so 8 % 5 = 3.

When 9 is divided by 5 the remainder is 4, so 9 % 5 = 4.

When 10 is divided by 5 the remainder is 0, so 10 % 5 = 0.

See how the remainders are cycling from 1, 2, 3, 4, 0, 1, 2, 3, 4, 0, ........ So we can say that anything when divided by 5 will give us the remainder in the range (0 to 4). x % 5 can be anything between 0 to 4. This observation will help us a lot in the future.

Since we have learned about the basic operations of math in programming.

Now it's time for some hands-on experience! I encourage you to solve a few problems and get your hands dirty with coding.