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
Step 1 Summary

I have  explained your queries in the form of comment in the code.

After adding some lines of code , I have provided  C++  CODE  along  with  CODE  SCREENSHOT  and  OUTPUT  SCREENSHOT---------

 

 

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
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
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage
Programming with Microsoft Visual Basic 2017
Programming with Microsoft Visual Basic 2017
Computer Science
ISBN:
9781337102124
Author:
Diane Zak
Publisher:
Cengage Learning