0% found this document useful (0 votes)
6 views2 pages

JavaScript Math Functions Examples

The document contains five JavaScript functions for basic arithmetic operations: addition, subtraction, multiplication, and division with modulus. Each function reads input from standard input, processes it, and outputs the result. The document also includes examples of post-increment and pre-decrement operations.

Uploaded by

sharmaakayy12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

JavaScript Math Functions Examples

The document contains five JavaScript functions for basic arithmetic operations: addition, subtraction, multiplication, and division with modulus. Each function reads input from standard input, processes it, and outputs the result. The document also includes examples of post-increment and pre-decrement operations.

Uploaded by

sharmaakayy12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Question-1:

function addNumbers(a, b) {
return a + b;
}

// Read input from stdin


const fs = require('fs');
const input = [Link](0, 'utf-8').trim();
const [a, b] = [Link](' ').map(Number);

// Output the sum


[Link](addNumbers(a, b));

Question-2

function subtractNumbers(a, b) {
return a - b;
}

const readline = require('readline');

const rl = [Link]({
input: [Link],
output: [Link]
});

[Link](' ', function (line) {


const [a, b] = [Link]().split(' ').map(Number);
const result = subtractNumbers(a, b);
[Link](result);
[Link]();
});

Question-3

function multiplyNumbers(a, b) {
return a * b;
}

// Read from stdin (works in judge platforms like ByteXL, CodeChef, etc.)
const fs = require('fs');
const input = [Link](0, 'utf-8').trim();
const [a, b] = [Link](' ').map(Number);
[Link](multiplyNumbers(a, b));

Question-4

function getQuotient(a, b) {
return [Link](a / b); // Floor division towards zero
}

function getModulus(a, b) {
return a % b; // JavaScript's default modulo
}

// Read input from stdin


const fs = require('fs');
const input = [Link](0, 'utf-8').trim();
const [a, b] = [Link](' ').map(Number);

// Output results
[Link](getQuotient(a, b));
[Link](getModulus(a, b));

Question-5
function increment(num) {
return num++; // Post-increment: returns current value, then increments
}

function decrement(num) {
return --num; // Pre-decrement: decrements first, then returns value
}

// Read input from stdin


const fs = require('fs');
const input = [Link](0, 'utf-8').trim();
const num = Number(input);

// Output results
[Link](increment(num));
[Link](decrement(num));

You might also like