Q:

C Program to reverse digits of a number

belongs to collection: C Programming on Numbers

0

In this blog post, we learn how to write a C Program to reverse the digits of a number? How to find reverse of any number using loop in C program. Logic to find the reverse of a number in C programming. How to write C program to reverse a number without using loop. How to write C program to reverse a number using array. How to write C program to reverse a number using function. Let’s see an example,

Input : num = 12345
Output : 54321
 
 
Input : num = 876
Output : 678

All Answers

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

#include <stdio.h>
int main()
{
    int number, reversed = 0;
    // Input a number from user
    printf("Enter any number = ");
    scanf("%d", &number);
    //Repeat the till number becomes 0
    while(number != 0)
    {
        /*
         * Increase place value of reversed and
         * add last digit to reversed
         */
        reversed = (reversed * 10) + (number % 10);
        // Remove last digit from number
        number /= 10;
    }
    printf("Reverse = %d", reversed);
    return 0;
}

 

Output:

Enter any number = 4562
Reverse = 2654

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

total answers (1)

C Programming on Numbers

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C Program to swap two numbers using ex-or operator... >>
<< C Program to find reverse of a number using a func...