Q:

Write a C Program to reverse a number entered by user

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Number Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to find Prime numbers between tw... >>
<< Write a C Program to check whether number is palin...