C program to display input digits in reverse order
Introduction:
This blog post will teach you how to write a C program to display input digits in reverse order. The program will accept the user's input and then print the number in reverse order.
Program coding:
#include<stdio.h>
int main()
{
int n,r,rev=0;
printf("enter any digit=");
scanf("%d",&n);
while(n>0)
{
r=n%10;
rev=rev*10+r;
n=n/10;
}
printf("\nReverse digit=%d",rev);
}
Program Explanation:
The number is first entered into the program by the user. The number is then reversed using a while loop. The program divides the number by 10 in each loop iteration and stores the remainder in the reversed_number variable. The variable reversed_number is then multiplied by 10 and the remainder is added to it. This procedure is repeated until the number reaches zero.
The program then prints the reversed number.
The output of the Program:
Conclusion:
We learned how to write a C program to display input digits in reverse order in this blog post. The program accepts the user's input and then prints the number in reverse order.
No comments:
Post a Comment