Skip to content

Reverse a number

JavaScript while

Same task in: C++TypeScriptGoRust

Flowchart (ISO 5807)

YesNoStartInput nr = 0n > 0r = r·10 + n mod 10n = Math.floor(n / 10)Return rEndFigure 1 — reverse

Source code

JavaScript
function reverse(n) {
    let r = 0;
    while (n > 0) {
        r = r * 10 + n % 10;
        n = Math.floor(n / 10);
    }
    return r;
}

The editor opens with this code — change it and the chart redraws.

Language
JavaScript
Constructs
while
Topic
Loops
Formats
Word, Visio, draw.io, PDF, SVG

Related examples