
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding Two Numbers Given Their Sum and Highest Common Factor Using JavaScript
Problem
We are required to write a JavaScript function that takes in two numbers. The first number represents the sum of two numbers and second represents their HCF (GCD or Greatest Common Divisor).
Our function should find and return those two numbers.
Example
Following is the code −
const sum = 12; const gcd = 4; const findNumbers = (sum, gcd) => { const res = []; if (sum % gcd !== 0){ return -1; }else{ res.push(gcd); res.push(sum - gcd); return res; }; }; console.log(findNumbers(sum, gcd));
Output
[4, 8]
Advertisements