Question:
How many numbers between 1000 and 9999 satisfy the condition that the thousands digit is equal to the sum of the hundreds, tens and ones digit?
Answer: 219
Solution: Solved using the script below
console.log("*************************************")
var count=0;
for(var i=0; i<10000; i++){
if(i>=1000){
var temp = i.toString();
tho = parseInt(temp[0]);
hun = parseInt(temp[1]);
ten = parseInt(temp[2]);
one = parseInt(temp[3]);
if(tho === hun+ten+one){
console.log(tho+" "+hun+" "+ten+" "+one);
count++;
}
}
}
console.log("count is "+count);
No comments:
Post a Comment