0% found this document useful (0 votes)
2 views2 pages

JavaScript Number Reversal and Armstrong Check

The document provides two JavaScript programs: one for reversing a number and another for checking if a number is an Armstrong number. The first program reverses the digits of a given number (e.g., 123 becomes 321) using a while loop. The second program checks if a number (e.g., 153) is equal to the sum of the cubes of its digits, indicating whether it is an Armstrong number.

Uploaded by

ayushmangukiya2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

JavaScript Number Reversal and Armstrong Check

The document provides two JavaScript programs: one for reversing a number and another for checking if a number is an Armstrong number. The first program reverses the digits of a given number (e.g., 123 becomes 321) using a while loop. The second program checks if a number (e.g., 153) is equal to the sum of the cubes of its digits, indicating whether it is an Armstrong number.

Uploaded by

ayushmangukiya2
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Write a JavaScript program enter one number and

reverse it:
Like number is 123 output 321.
let number = 123;

let reverse = 0;

while (number > 0) {

let digit = number % 10;

reverse = reverse * 10 + digit;

number = parseInt(number / 10);

document. write ("Reversed number is: " + reverse);

Step-by-step Trace (number = 123)

reverse number after


digit
Step number (reverse * 10 parseInt(number /
(number %10)
+ digit) 10)
1 123 3 0 * 10 + 3 = 3 12
2 12 2 3 * 10 + 2 = 32 1
3 1 1 32 * 10 + 1 = 321 0 → loop ends
Write a JavaScript program and check Armstrong
or not. Armstrong means 153=1+125+9(cube)=153
<script>

let number = 153;

let original = number;

let sum = 0;

while (number > 0) {

let digit = number % 10;

sum = sum + (digit * digit * digit);

number = parseInt(number / 10);

if (sum == original) {

[Link]("number is an Armstrong number");

} else {

[Link]("number not an Armstrong number");

}
digit = number % sum = sum + number = parseInt(number /
Step number 10
digit³
digit³ 10)
1 153 3 27 0 + 27 = 27 15
2 15 5 125 27 + 125 = 152 1
3 1 1 152 + 1 = 153 0 (loop ends)

You might also like