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?
Control structures
Control structures are block of statements that analyze the value of variables and determine the flow of execution based on those values. When a program is running, the CPU executes the code line by line. After sometime, the program reaches the point where it has to make a decision on whether it has to go to another part of the code or repeat execution of certain part of the code. These results affect the flow of the program's code and these are called control structures.
Switch Statement
The switch statement is a key feature that is used by the programmers a lot in the world of programming and coding, as well as in information technology in general. The switch statement is a selection control mechanism that allows the variable value to change the order of the individual statements in the software execution via search.
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;
}
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---------
Step by step
Solved in 4 steps with 3 images