I'm writing a program to check the number whether it is armstrong or not. And I'm unable to find the,what is mistake?

huangapple go评论57阅读模式
英文:

I'm writing a program to check the number whether it is armstrong or not. And I'm unable to find the,what is mistake?

问题

const Armstrong_number = (x) => {
  let sum = 0;
  while (x > 0) {
    let break_number = x % 10;
    let power = break_number ** c;
    sum += power;
    x /= 10;
  }
  if (sum == x) {
    console.log("It is Armstrong number");
  } else {
    console.log("It is not an Armstrong number");
  }
}
let number = prompt("Enter a number");
let c = number.length;
number = Number.parseInt(number);
Armstrong_number(number);

我已经将您提供的JavaScript代码翻译出来了。如果您需要进一步的帮助,请随时告诉我。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const Armstrong_number = (x) =&gt; {
  let sum =0;
  while(x&gt;0){
    let break_number = x%10;
    let power = break_number**c;
    sum += power;
    x/=10;
  }
  if(sum==x){
    console.log(&quot;It is Armstrong number&quot;);
  }
  else 
  {
    console.log(&quot;It is not an Armstrong number&quot;);
  }
}
let number = prompt(&quot;Enter a number&quot;);
let c = number.length;
number = Number.parseInt(number);
Armstrong_number(number);

<!-- end snippet -->

I have tried 153 and it is showing it is not an Armstrong number (a number that equals the sum of its own digits each raised to the power of the number of digits).I'm trying to check armstrong for n numbers

答案1

得分: 2

Here's the translated code snippet:

const Armstrong_number = (x) => {
  let sum = 0;
  let X = x;
  while (X > 0) {
    let break_number = X % 10;
    let power = break_number ** c;
    sum += power;
    X = Math.floor(X / 10);
  }
  if (sum == x) {
    console.log("它是阿姆斯特朗数");
  } else {
    console.log("它不是阿姆斯特朗数");
  }
}
let number = prompt("输入一个数字");
let c = number.length;
number = Number.parseInt(number);
Armstrong_number(number);

Note: I've translated the code part as requested, excluding any explanation or additional information.

英文:

There's a couple of things here.

Firstly, you're modifying your input x. Your while loop continues until x is 0 – but then you compare sum to x (which is now zero!), when you really wanted the original value of x.

This is something you could spot during debugging by checking that the values you're comparing are what you'd expect. You could put in a breakpoint on the line containing sum==x, or even just console.log(sum, x) on the line before the comparison and checking that the values were what you'd expect.

The other thing is that Javascript numbers use floating point arithmetic, so when you divide x by 10 it is not discarding the last digit (as it would in, say, C language).

Considering your test input 153, the value of x in the loop goes:

x = 153
x = 15.3
x = 1.53
x = 0.153
x = 0.0153
// etc

until eventually it gets small enough that it rounds to zero.

This is also something that you can spot during debugging, just by adding a console.log(x) in the loop. You could even just add a counter that increments each time through the loop, and check at the end that the total count matches what you expected (i.e. the number of digits in the input).

The fixes are easy enough – operate on a copy of x, and truncate to an integer after dividing by ten:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

const Armstrong_number = (x) =&gt; {
  let sum =0;
  let X = x;
  while(X&gt;0){
    let break_number = X%10;
    let power = break_number**c;
    sum += power;
    X = Math.floor(X / 10);
  }
  if(sum==x){
    console.log(&quot;It is Armstrong number&quot;);
  }
  else 
  {
    console.log(&quot;It is not an Armstrong number&quot;);
  }
}
let number = prompt(&quot;Enter a number&quot;);
let c = number.length;
number = Number.parseInt(number);
Armstrong_number(number);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年4月13日 16:35:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003350.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定