Powered By Blogger

Tuesday 17 March 2020

C Programme to Reverse a Number using While Loop And Arrray functions


Print numbers in Reverse Order In ‘C’ programming using While Loop & Array functions.............


Programming Code for printing Reverse Number in 'C
Reverse number printing in 'C'



Welcome viewers to my new blog on how to print numbers in reverse order in ‘C’ programme. Today we will deal with this interesting programme and coding with ‘C’. This is also a simple programme in ‘C’ but it needs some understanding in the compilation of the programme. So, without wasting time lets start the programme.

In this programme, we will use the “Modulus(%)” function to some extent. The property of the function will be described in its respective position.

  • First, we are coming to the variable part :
We have taken three variable for this programme i.e num,num1 and rev.
‘num’ denotes ‘number’ which we will take the input from the user.
‘num1’ is another variable to apply a modulus function,. We will see its use in the programming part.
‘rev’ denotes ‘reverse’ which saves the Reverse Value of the given number.

  • Second, The programming part :

Before starting let's talk about the ‘Modulus’ function in ‘C’ briefly.
‘Modulus’ function is nothing but the remainder of a division process.
Let's take an example:
If we take a number – 1234 and divide it with 10.
Then, 1234%10==4 that is the remainder of the division.
If we divide 542 with 5, then 542%5==2.
i.e. in programming  int x;
                                     x=542%5;
                                     printf(“%d”,x);
              Output:   2

Here, we have used a ‘while loop’ to perform this programme. It is given with a condition “num!=0”.
It means until the value of ‘num’ become equals to zero, the loop iterates.

Inside the loop, we have given an argument  “num1=num%10”  It means after the calculation of num%10, the value will be stored as num1 and each time the while loop iterates the value of the variable ‘num’ changes according to the argument given inside the loop.  This argument is given because we are trying to print the given number into reverse order, so we use modulus function it gives us the remainder of the division and thus giving us the last most digit of the number as we are the dividing the number with 10. It is discussed clearly in the next argument section.

Next, argument is “num=num/10”. It should be noted that the variable ‘num’ is taken as an integer value. So, after diving a number with 10 it will save only the integer value, not floating-point value.
If we take a number – 5421
Then 5421/10==542(integer value) not 542.1(floating point value).
This condition plays a vital role in the ‘while loop’ iteration and also in performing the previous argument.
Before going to the third argument part, we will take some examples of how the above mentioned two arguments are working in this programme.
If we take a number- 1347. So, num=1347
Then,
1st argument: “num1=num%10”
So, num1=1347%10==7. That means we are getting the last most digit of the given number by the ‘Modulus function’.
Moving to the next argument
2nd argument: “num=num/10”
So, num=1347/10==134 (the only integer value is stored as num)
After completing of the first loop iteration, the while condition is checked i.e ‘num!=0’  (‘!’- not equal to), if the condition is true the loop iterates otherwise it gets out of the loop.
For the taken example loop will iterate a total of 4 times.
For 2nd iteration: The present value of ‘num’=134
1st argument: num1=134%10==4. That means we are getting the second digit from the backside of the number.
2nd argument: num=134/10==13 (not 13.4)
For 3rd iteration: The present value of ‘num’=13
1st argument: num1=13%10==3. That means we are getting the third digit from the backside of the number.
2nd argument: num=13/10==1
For 4th and the last iteration: The present value of ‘num’=1
1st argument: num1=1%10==1. That means we are getting the fourth digit from the backside of the number or the first digit of the number.
2nd argument: num=1/10==0

And after the completion of 4th iteration the value of ‘num’==0, so ‘while loop’ condition is violated and thus the iteration of loop stops.
Now, coming to the most important the 3rd argument. It is basically doing the job making the number in reverse order. Let's see the argument

rev=num1+(rev*10)
For 1st  iteration of ‘while loop’ :  num=1347 , num1=7
So, rev=7+(0*10) . In the variable declaration section we have taken the initial value of ‘rev’=0.
rev==7
For 2nd  iteration of ‘while loop’ :  num=134 , num1=4 ,rev=7
rev=4+(7*10)==74
For 3rd  iteration of ‘while loop’ :  num=13 , num1=3 ,rev=74
rev=3+(74*10)==743
For 4th and the last iteration of ‘while loop’ :  num=1 , num1=1 ,rev=743
rev=1+(743*10)==7431
Then the value of num becomes zero as 1/10==0( again remember num is taken as an integer value)
‘while loop’ condition now becomes false and it stops to iterate.

Thus, the final value of ‘rev’ becomes 7431 i.e the reverse order of the given number 1347.
This is how the whole process proceeds. The entire programming code is given below:

C programme to reverse a number using while loop
Programming Code for printing Reverse Number in 'C'




This programme can be done with the use of ‘ARRAY’. Stay tuned for it. Hope, you understand the explanation and the programme. Please, share and comment if this helps you. If any question, please comment below. Thank you for reading.......

No comments:

Post a Comment

Can we use pointers without specifying the data type of the pointer?

Void Pointer Yes, we can use pointers without specifying the data type of the pointers. But remember at the end of the programming, wh...