Learn about C Program to Find Reverse of a Number using for 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 for loop
- Declare and initialise the following three variables: int number,rem,reversed Num=0;
- The user is prompted to provide a number, which is saved in the integer variable ‘number.’
- The for loop is used to find the inverse of a given number.
- The for loop will continue to run 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;
printf("Enter a number: ");
scanf("%d",&number);
for(reversed=0; number!=0; number /= 10)
{
rem = number%10;
reversed = (reversed*10) + rem;
}
printf("Reverse Number = %d\n",reversed);
return 0;
}
Output:
Enter a number: 456
Reverse Number = 654
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 using the do-while loop
C Program to Find Reverse of a Number