TypeScript: Find the Remainder of Two Numbers

1. Introduction

When working with numbers, sometimes you may need to find the remainder after performing a division operation. In TypeScript, the modulus operator (%) allows us to do this effectively.

2. Program Overview

In this program, we'll define a function named findRemainder that will take in two numbers as arguments. It will then return the remainder after dividing the first number by the second number.

3. Code Program

// Define the function with TypeScript type annotations
function findRemainder(num1: number, num2: number): number {
    return num1 % num2;
}

// Test the function
const result = findRemainder(7, 3);
console.log(`The remainder of 7 divided by 3 is: ${result}`);

Output:

The remainder of 7 divided by 3 is: 1

4. Step By Step Explanation

The modulus operator (%) returns the remainder left after the division of two numbers. In the example above, when 7 is divided by 3, it goes 2 times completely making it 6, and 1 remains. Thus, the remainder of 7 divided by 3 is 1.

Comments