Write a C Program to reverse a number entered by user

By | 10.10.2016

C Program  to reverse a number entered by user


Write a C program to enter any number from user and find the reverse of given number using for loop. How to find reverse of any number in C programming using loops. Program to find reverse of a given number.

C Program to reverse a number :- This program reverse the number entered by the user and then prints the reversed number on the screen.

Example:

Input number: 1234
Output reverse: 4321

Logic to find reverse :

The process of reversing involves four basic steps:

  1. Multiply the rev variable by 10.

  2. Find the last digit of the given number.

  3. Add the last digit just found to rev.

  4. Divide the original number by 10 to eliminate the last digit, which is not needed anymore.

Repeat the above four steps till the original number becomes 0 and finally we will be left with the reversed number in rev variable.

Here is source code of the C program to reverse a number . The C program is successfully compiled and run(Codeblocks) on a Windows system. The program output is also shown below.

 


SOURCE CODE : :


#include<stdio.h>
#include<math.h>
int main()
{
long int num, rev = 0, dig;

printf("\n\nENTER A NUMBER-----: ");
scanf("%ld", &num);
while(num>0)
{
dig = num % 10;
rev = rev * 10 + dig;
num = num / 10;
}
printf("\nREVERSED NUMBER IS----: %ld", rev);
return 0;
}

 


OUTPUT : :


ENTER A NUMBER-----: 286734

REVERSED NUMBER IS----: 437682
5 1 vote
Article Rating
Category: C Programming Number Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments