Saturday, 12 September 2020

A Program to print given number in reverse order

 


Program to print given number in reverse order

#include<stdio.h>
#include<conio.h>

int main() {
int num, x, rev = 0, dummy;

printf("Enter a number: ");
scanf("%d", &num);
        dummy = num;

while(num>0) {
x = num%10;
rev = rev * 10 + x;
num = num/10;
}
printf("The reverse of %d is %d", dummy, rev);
return 0;
}

Output




Variables Used:

  1. num: num is used to store the number given by the user.
  2. x: x is used to store num temporarily after finding modulus of num with 10 and dividing num by 10.
  3. rev: rev is used to store the reversed value of the given number (final output) whose value is initially 0.
  4. dummy: dummy is used to store the value of num just for showing the value of num in the output screen
            note: num value is changed again and again inside the while block so it doesn't 
                      stores it's actual value that was given by user. (dummy stores it).

Description

  1. Firstly we ask the value of num to the user.
  2. Then, we store the value of num in dummy just to use it at the last while printing the previous value of num.
  3. Then, if x value is greater than 0 we go through while loop
  4. In the first line inside while loop x = num % 10, x stores modulus of num after dividing it to 10.
  5. In the second line inside while loop rev = rev * 10 + x, here rev stores value after multiplying 10 with rev then adding x. likely it stores the final value.
  6. In the third line inside the while loop num = num/10, it divides num/10 and num stores int value of reminder.
  7. Lastly printf prints the final reversed value of given number.

working Module:


if num = 456, the
  • firstly dummy stores the value of num then it goes to while loop and checks condition if num is greater than 0 or not. Here num 456 is greater than 0 so this condition returns true so it goes inside while loop. Compiler checks the first line of the block where the logic is x = num % 10 = 456%10 = 6. It calculates the modulus of num and gets the value as 6 and stores it in x variable then it goes to second line and the logic is rev = rev * 10 + x. In this logic variable x is treated as 6 just because x stores 6. Then it performs the calculation as rev = 0 * 10 + 6 = 6 ( rev is initially 0), now rev stores 6 then it goes to third line and checks the logic num = num/10. This logic is performed as num = 456/10 = 45 . Now num stores 45 (it doesn't store 45.6 because it's data type is int (integer)). Then again it goes to while loop and checks condition (num>0), yes num is 45 now and it is still greater so the condition returns true again. It goes to first line again and performs same task as before. this time x stores x = num % 10 = 45 % 10 = 5. Now compiler goes to second line and now rev stores rev = rev * 10 + x = 6 * 10 + 5 = 65. Then it goes to third line and now num stores num = num / 10 = 45/10 = 4. Now it again checks the condition inside while loop (num>0) yes num = 4 is greater than 0 which is true so it again goes inside while loop and read the first line x = num % 10 = 4%10 = 4. Then it goes to second line rev = rev * 10 + x = 65 * 10 + 4 = 654. Then it compiler checks third line and calculate num as num = num/10 = 4/10 = 0. again it goes to while loop and checks condition (num>0) but this time it returns false because num is smaller than 0. Now finally rev stores 654 which is the reversed value of 456 then we print it using printf function.





1 comment:

  1. Sir I really appreciate you. You really taught me clearly. You made so easy to learn C. Heartly thank you sir.

    ReplyDelete

If you have any doubt, please ask in the comment section.

Search This Blog

Program to print weather the number is happy or not.

#include <stdio.h> #include <math.h> int main () { int i , j , num , temp , sum = 0 ; printf ( "Enter number\n...