Search This Blog


Friday, 19 April 2019

C Program to Reverse a Given Number

Welcome back to codejunction.in , glad to see you again on our new blog on reversing a given number.


This C program accepts an integer and reverse it.

Problem Solution
1. Take the number which you have to reverse as the input.
2. Obtain its quotient and remainder.
3. Multiply the separate variable with 10 and add the obtained remainder to it.
4. Do step 2 again for the quotient and step 3 for the remainder obtained in step 4.
5. Repeat the process until quotient becomes zero.
6. When it becomes zero, print the output and exit.

CODE
#include <stdio.h>
 
void main()
{
long num, reverse = 0, temp, remainder;
 
printf("Enter the number\n");
scanf("%ld", &num);
    temp = num;
while (num > 0)
{
        remainder = num % 10;
        reverse = reverse * 10 + remainder;
        num /= 10;
}
printf("Given number = %ld\n", temp);
printf("Its reverse is = %ld\n", reverse);
}


OUTPUT


No comments:

Post a Comment