Here's a C program to reverse an integer with output and proper explanation. This program uses while loop.
# include <stdio.h> # include <conio.h> void main() { long n, r, s = 0 ; clrscr() ; printf("Enter a number : ") ; scanf("%ld", &n) ; while(n > 0) { r = n % 10 ; s = r + s * 10 ; n = n / 10 ; } printf("\nThe reversed number is : %ld", s) ; getch() ; }
Output of above program
Enter a number : 123
The reversed number is : 321
Explanation of above program
This program takes a number and displays its reverse. Also this program uses variables of type long.
Note: The long data type has a size of 4 bytes which is more than int data type's size of 2 bytes. So, the use of long data type enables the user to enter much bigger numbers.
Here we have 3 variables all of the data type long -
- n - is the number that user enters and whose reverse we're finding.
- r - is used as a temporary variable to store the remainder (we'll see it's use in a minute).
- s - is the reverse of n. It is initialized to 0 at the beginning of program.
The reverse of the number is calculated inside the while loop. The while loop here has three statements inside it. Let's understand how while loop works here to determine the reverse of a number with the help of an example.
Suppose n = 123. The process of while loop for n = 123 is as follows -
- First n > 0 i.e. 123 > 0 so the program enters the while loop.
- Then we calculate modulo 10 of our number and store its value in r i.e. r = n % 10 = 123 % 10 = 3 (since we're calculating modulo 10, the result is simply the last digit of n).
- Next we're calculating the value of s using the following expression - s = r + s * 10. Therefore using old values of r and s the new value of s is - s = 3 + 0 * 10 = 3.
- And finally we divide n by 10 and storing its value again in n itself i.e. n = n / 10 = 123 / 10 = 12 (since the type of n is long int so the fractional part is dropped from the result).
- Now that all statements inside the loop have been executed once the looping condition is again checked.
- Again n > 0 i.e. 12 > 0 so the loop will execute again following all the steps mentioned above.
- Then we calculate the value of r i.e. r = n % 10 = 12 % 10 = 2 (again the last digit of n).
- The new value of s is s = r + s * 10 = 2 + 3*10 = 32.
- Now we divide n by 10. Therefore, n = 12 / 10 = 1.
- Again the looping condition is checked and the loop is executed one more time with n = 1 and s = 32.
- Then we calculate the value of r i.e. r = n % 10 = 1 % 10 = 1 (again the last digit of n).
- The new value of s is s = r + s * 10 = 1 + 32*10 = 321 (reverse of n).
- Now we divide n by 10. Therefore, n = 1 / 10 = 0.
- Now the looping condition becomes false and the loop terminates printing the final result.
Note: If you’d notice the printf() and scanf() in this example you’d see that I wrote %ld not %d. The reason behind this is that we’re working with variables of data type long (long int) not just int. So, while we use %d for int type, we’re using %ld to print value of long (and in case of scanf to take long as input) here.
HI There,
Seems like I won the lottery here….This is a treasure box of blogs and your folks are like leprechauns! Phenomenal read on Program for Reversing an Integer in C | C Program!
Is it possible to read two lines of a file in one iteration based on some condition? The files has several blocks of data where each block contains two consecutive lines having ORDER ID in it. I want to store each of these IDs for each block of data in two different variables e-g
Code:
I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.
Kind Regards,
Preethi