Learn about C Program to Find Reverse of a Number using the do-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 the do-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 do-while loop is used to calculate the inverse of a given integer.
- The do-while loop runs until ‘
number
‘ is not equal to zero. - Finally, the output is shown as a reversed number.
Source code:
#include<stdio.h>
int main()
{
int number,rem, reversed=0;
printf("Enter a number: ");
scanf("%d",&number);
do
{
rem = number%10;
reversed = (reversed*10) + rem;
number /= 10;
}while( number!=0 );
printf("Reverse Number = %d\n",reversed);
return 0;
}
Output:
Enter a number: 321
Reverse Number = 123
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 Find Reverse of a Number
C Program to Find Reverse of a Number using while loop