- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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]
- Related Articles
- Program to find HCF (Highest Common Factor) of 2 Numbers in C++
- Finding the sum of all common elements within arrays using JavaScript
- Print the kth common factor of two numbers
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Finding lunar sum of Numbers - JavaScript
- Finding closest pair sum of numbers to a given number in JavaScript
- Finding sum of remaining numbers to reach target average using JavaScript
- Finding pandigital numbers using JavaScript
- Finding two golden numbers in JavaScript
- Finding two numbers that produce equal to the sum of rest in JavaScript
- Finding the common streak in two arrays in JavaScript
- Choose the correct option for the following statement:The LCM of two or more given numbers is the Highest of their common factors.(A) True(B) False
- Finding the sum of two numbers without using '+', '-', '/', '*' in JavaScript
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- The sum of two numbers is 8. If their sum is four times their difference, find the numbers.

Advertisements