Learn about C Program to Find Reverse of a Number using while loop in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
C Program to Find Reverse of a Number using while loop
Explanation:
Declare and initialize the following three variables: int number, rem, reversed =0;
The user is prompted to provide a number, which is saved in the integer variable ‘number’.
The while loop is used to calculate the inverse of a given integer.
The while loop runs until ‘number’ is not equal to zero.
Finally, the output is shown as a reversed number.
Program:
#include<stdio.h>
int main()
{
int number,rem, reversed=0;
printf("Enter a number: ");
scanf("%d",&number);
while( number!=0 )
{
rem = number%10;
reversed = (reversed*10) + rem;
number /= 10;
}
printf("Reverse Number = %d\n",reversed);
return 0;
}
Output:
Enter a number: 521
Reverse Number = 125
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
C program to solve the quadratic equation
C program to check odd or even using bitwise operator