- 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

# Count of N-digit Numbers having 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 A, B and N, and finds the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively.

## Example

Let’s write the code for this function −

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(2, 5, 3));

## Output

Following is the output in the console −

[ 30, 35, 60, 65, 90, 95 ]

- Related Articles
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Count n digit numbers divisible by given number in C++
- Count of n digit numbers whose sum of digits equals to given sum in C++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Largest Even and Odd N-digit numbers in C++
- Check whether product of digits at even places is divisible by sum of digits at odd place of a numbers in Python
- Squared sum of n odd numbers - JavaScript
- Print all n-digit numbers whose sum of digits equals to given sum in C++
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Largest N digit number divisible by given three numbers in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Count n digit numbers not having a particular digit in C++
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++

Advertisements