May you help me trace this code for my understanding. This code will print out the reverse of a number. My question is could you trace what each line does. for example if the user input was 321, when the code reaches while(num>1); before if(num>=1) what is stored in the variable reserved? is it the individual values 1 2 3 and when the code reaches if(num>=1) what is reversed and what is being multiplied by 10? is it the individual values 1 2 3?

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 9SA
icon
Related questions
icon
Concept explainers
Question

May you help me trace this code for my understanding. This code will print out the reverse of a number. My question is could you trace what each line does. for example if the user input was 321, when the code reaches while(num>1); before if(num>=1) what is stored in the variable reserved? is it the individual values 1 2 3 and when the code reaches if(num>=1) what is reversed and what is being multiplied by 10? is it the individual values 1 2 3? 

 

code:

int reversedNumber(int num){
    if(num==0){
        return 0;
    }
    int reversed =0;
    while(num>1){
        reversed +=num%10;
        num/=10;
        if(num>=1)
        reversed *=10;
    }
    if(num==1)
    reversed+=1;
    return reversed;
}

Expert Solution
steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Control Structure
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning