Now let's see a C program to print reverse of a given number. That means let the number is 786. So after reversing 786, what is the output? 687 is the output. Let the number is 12. So after reversing 12, the output is 21. So here the input is a number whereas the output is reverse of that number. Let us assume that here the number is 1234. Let us assume that the number is 1234. So the reverse of 1234 is 4321. So we have to display 4321 as the output.
Here if the number contains 2 digits then 2 iterations are needed. Whereas if the number contains 3 digits then 3 iterations are needed. In this example the number contains 4 digits. So totally we need to have 4 iterations. So let us see the logic here.
First from 4, from 1, 2, 3, 4, we have to extract a digit. From 1, 2, 3, 4, it is better, it is very, very easy to extract the last digit. That is nothing but 4. If you perform modulo 10 operation, then we will get the last digit.
That is nothing but 1, 2, 3, 4. The last digit, that is nothing but 4. Let us perform the operation. So, 10, 1, 2, 3, 4. Here 10, 1s are 10. Next directly we can write here. So 10, 1s are 10 next to 23. 10, 20s are 20, 3, 4. So 10, 30s are 30. That is nothing but 4. So here what is the remainder here? 4 is the remainder.
So 10, 1, 2, 3 is nothing but the quotient. So 10 into 1, 2, 3, 0 is nothing but 1, 2, 3, 0, 4. so if we perform modulo 10 then we will get 4 whereas if we perform division 10 we will get the quotient here the quotient is 123 remainder is 4 okay division operation will give quotient whereas modulo operation will gives the remainder here the logic is very very simple reb is equal to rev into 10 plus remainder the initial value of reb is 0 So 0 into 10 plus, so what is the initial remainder? 4. So 0 into 10 means 10, 10 plus 4 means 4. So that means after the first iteration, we will get the last digit as the output.
After second iteration, the last two digits are reversed. That means we will get 43 as the output. After the third iteration, we will get the reverse of the last three digits. That is nothing but 432. In the last iteration, we will get 4, 3, 2, 1 as the output.
Now, the operation on 4 is over. So, in the next iteration, there is no need of 4. So, we need to have 123. So, how to get 123 from 1, 2, 3, 4? By performing division 10 operation.
So, now in the first iteration, n value is 1, 2, 3, 4. Whereas in the second iteration, n value is 123. So, 10th pulse are 120. So 123 minus 120 means 3. So now what is the remainder value? 3 is the remainder value. So REV is equal to, what is the formula?
REV into 10. What is REV value? 4. So 4 into 10 plus what is the remainder value now? 3. So 4 into 10 means 40. 40 plus 3 means 43. That is nothing but reverse of the last two digits.
So in the second iteration, operation on 3 is over. So, in the next iteration, there is no need of 3. We have to perform operation only on 12. So, how to get 12 from 123? That is nothing but by division, by 10. So, in the third iteration, n value will become 12. So, 10 ones are 10s. So, 12 minus 10 is 2. So, now the remainder value is 2. So, let us calculate now. REV is equal to REV into 10. The previous value of REV is 43. So, 43 into 10 plus.
now remainder value is 2 so 43 into 10 means 430 430 plus 2 is nothing but 432 432 means reverse of the last three digits okay next operation on 2 is over so now there is no need of this 2 in the next iteration okay so how to get this one by performing division 10 okay so 10 1 so 10 zeros are 0 so 1 minus 0 means 1 so now the remainder value is 1 So, R e b is equal to R e b value is 432. 432 into 10 plus what is the remainder? 1. So, that is nothing but 4, 3, 2, 1. So, now what is n value? 0. So, we have to repeat the loop as long as n is not equal to 0. So, whenever n is equals to 0, then we have to quit from the loop.
So, what is Re value? 4, 3, 2, 1. So, that is nothing but the reverse of this number. The reverse of 1, 2, 3, 4. Now, we have to write a program in order to implement reverse of a given number.
So, let us write the program. The first statement is hash include std evo.h. Why?
Because we are using printf and scanf functions. Printf and scanf are available in std evo. Next, every C program execution starts from main function.
Next, the variables which are needed are, we need to have a number. For number, let us use n. Next, in order to calculate the remainder, use r. Next, in order to calculate tables of a given number, we need to have rev. the initial value of rev is zero okay next before reading n value let us display some message on the monitor so print of enter again print of enter n next scan of here n is of type integer for integer the format specified is percentage d where d stands for decimal number decimal number is nothing but integer number only ampersand n next what is the condition here we have to perform the repeating function As long as n is not equals to 0. So, while n is not equals to 0. So, if n is not equals to 0, then we have to perform the repetition.
So, first what we have to do, first we need to calculate the remainder. What is the formula for remainder? r is equal to n mod 0 10. Why? Because modular will always use the remainder.
So, r is equal to n mod 0 10. Next, what is the formula for calculating reverse? REV is equal to REV into 10 plus R. So that is the formula here. So REV is equal to REV into 10 plus R.
In the first iteration R value is 4. In the second iteration R value is 3. In the third iteration R value 2. In the last iteration R value 1. Likewise. Next what is n value? n value should be 123. So how to get 123 from 1, 2, 3, 4? That is nothing but division 10. so n is equal to n is equal to n by 10 so whenever the condition is false then reverse of the number is available in rev so printf so display the message printf printf reverse of number is reverse of number is percentage v comma so result is available in rev next to close the main function okay now let's check whether our logic is correct or not we know that execution starts from the main function The initial value of R e v is 0. Next, printf, enter a number.
Scanf %d, %n. Let the number is 1, 2, 3, 4. While n is not equal to null, so what is n value? 1, 2, 3, 4. So, 1, 2, 3, 4 is not equal to null. I am sorry. 1, 2, 3, 4 is not equal to 0. Condition is true.
So, the body will be exit located. R is equal to n modulo 10. So, what is 1, 2, 3, 4 modulo 10? 4. So, now R value is 4. Next, reverse is equal to reverse into 10 plus r. What is the previous value in reverse? 0. So, 0 into 10 plus what is r value?
4. So, 0 into 10 means 0. 0 plus 4 means 4. Next, n is equal to n by 10. 1, 2, 3 by 1, 2, 3, 4 by 10 is nothing but 1, 2, 3. So, now n value is 1, 1, 2, 3. So, now the first iteration is over. So, after first iteration, what is the output? The last digit in 1, 2, 3, 4. In 1, 2, 3, 4, what is the last digit? 4 is the last digit.
Okay. So, that is the output here. Okay.
So, next control goes to the value. Yn is not equal to 0. Now, n value is 1, 2, 3. So, 1, 2, 3 is not equal to 0. Condition is true. So, what will be executed?
So, it will be executed now. R is equal to n modulo 10. 1, 2, 3 modulo 10 means 3. So, now R value is 3. REV is equal to REV into 10 plus R. So, what is REV value?
4. so 4 into 10 plus what is our value 3 4 into 10 means 40 40 plus 3 means 43 so next n is equal to n by 10 1 2 3 by 10 means 12 so now n value is 12 the second iteration is over after second iteration what is the output 43 43 means we were soft the last two digits so once again control goes to the condition now what is n value 12 12 is not equal to 0 condition is true So, R is equal to n mod 0 10. So, this is over. Now, it will be executed. So, what is 12 mod 0 10?
2. Next, now R value is 2. R e v is equal to R e v into 10 plus R. So, 43 into 10 plus what is R value? 2. So, 43 into 10 means 430. 430 plus 2 means 432. Next, n is equal to n by 10. 12 by 10 means 1. So, now n value is 1. So, third iteration is over. What is R e v value?
432. 432 means the reverse of the last three digits so once again control goes to the condition y n is not equal to 0 now n value is 1 1 is not equal to 0 condition is true so now it will be executed r is equal to i'm sorry it will be executed r is equal to n modulo 10 1 modulo 10 means 1 okay so now r value is 1 rev is equal to rev into 10 plus remainder what is the remainder 432 into 10 means 4320 plus 1 means 4321. Next, n is equal to n by 10. 1 by 10 means 0. Now, n value is 0. Now, control goes to the condition. 0 is not equal to 0. Condition is false. So, control comes out from the value. Reverse of the number is in place of percentage d.
RGB value will be printed. What is RGB value? 4321 will be printed.
So, in this way, we can print reverse of a given number.