

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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]
- Related Questions & Answers
- Print the kth common factor of two numbers
- Program to find HCF (Highest Common Factor) of 2 Numbers in C++
- C program to find Highest Common Factor (HCF) and Least Common Multiple (LCM)
- Finding the sum of all common elements within arrays using JavaScript
- Finding lunar sum of Numbers - JavaScript
- Finding two golden numbers in JavaScript
- Finding the common streak in two arrays in JavaScript
- Finding pandigital numbers using 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 two numbers that produce equal to the sum of rest in JavaScript
- Program to find highest common factor of a list of elements in Python
- Finding the longest common consecutive substring between two strings in JavaScript
- Find two numbers whose sum and GCD are given in C++
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
Advertisements