- 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 all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
We are required to write a JavaScript function that takes in three numbers. Let's say the three numbers are a, b and n.
Our job is to find all the n-digit numbers whose sum of digits at even positions and odd positions are divisible by a and b respectively. And we have to lastly return an array containing all the required numbers, the array should be empty if there are no matching numbers.
Example
Following is the code −
const indexSum = (num, sumOdd = 0, sumEven = 0, index = 0) => { if(num){ if(index % 2 === 0){ sumEven += num % 10; }else{ sumOdd += num % 10; }; return indexSum(Math.floor(num / 10), sumOdd, sumEven, ++index); }; return {sumOdd, sumEven}; }; const divides = (b, a) => a % b === 0; const countNum = (n, first, second) => { let start = Math.pow(10, (n-1)); const end = Math.pow(10, n)-1; const res = []; while(start <= end){ const { sumEven, sumOdd } = indexSum(start); const condition = divides(first, sumEven) && divides(second,sumOdd); if(condition){ res.push(start); }; start++; }; return res; }; console.log(countNum(3, 5, 3));
Output
This will produce the following output in console −
[ 104, 109, 134, 139, 164, 169, 194, 199, 203, 208, 233, 238, 263, 268, 293, 298, 302, 307, 332, 337, 362, 367, 392, 397, 401, 406, 431, 436, 461, 466, 491, 496, 500, 505, 530, 535, 560, 565, 590, 595, 604, 609, 634, 639, 664, 669, 694, 699, 703, 708, 733, 738, 763, 768, 793, 798, 802, 807, 832, 837, 862, 867, 892, 897, 901, 906, 931, 936, 961, 966, 991, 996 ]
- Related Articles
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Largest Even and Odd N-digit numbers in C++
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Count n digit numbers divisible by given number in C++
- Counting n digit Numbers with all unique digits in JavaScript
- Find the sum of all 2-digit natural numbers divisible by 4.
- N digit numbers divisible by 5 formed from the M digits in C++
- Check whether product of digits at even places is divisible by sum of digits at odd place of a numbers in Python
- Largest N digit number divisible by given three numbers in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Squared sum of n odd numbers - JavaScript
- State whether the following statements are True or False:(a) The sum of three odd numbers is even.(b) The sum of two odd numbers and one even number is even.(c) The product of three odd numbers is odd.(d) If an even number is divided by 2, the quotient is always odd.(e) All prime numbers are odd.(f) Prime numbers do not have any factors.(g) Sum of two prime numbers is always even.(h) 2 is the only even prime number.(i) All even numbers are composite numbers.(j) The product of two even numbers is always even.
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Find the sum of all 3-digit natural numbers which are divisible by 13.

Advertisements