Introduction to Basic Math
In this section, we will cover every basic concept of math that you will require on your programming journey.
A number comprises one or more digits arranged in a specific order. The arrangement and the base of the numeral system determine the value of the number.
In the standard base-10 (decimal) system, which is the most commonly used numeral system, there are ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Digits are the building blocks for numbers. For example, the number 547 is composed of the digits 5, 4, and 7. You will read this as Five thousand four hundred and seven.
So now if you are told to do some basic operations like addition, subtraction, multiplication, division, and modulo.
How will you be doing that while 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 an operator like ‘+’.
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 an operator like ‘-’.
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 an operator like ‘*’.
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 an operator like ‘/’.
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. Here's an example:
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.
Since we have learned about the basic operations of math in programming.
Now let’s start with learning these sections with basic questions.
So let’s take a question in the digit of a number.