Write a C program to print reverse of a given Number

If you are a student and searching for the Write a program to print reverse of a given number. Then you are at the right place.

In this tutorial, I will teach you and provide you the C Program to print reverse of a given number.

To understand the given program, you might need a little bit programming knowledge, if you already know, you are ready to go.

{tocify} $title={Table of Contents}

Write a C Program to print reverse of a given number

Write a C program to print reverse of a given Number

Here is the C Program given below, and detailed explanation is given below the program.

C Program for reverse of a given number

#include <stdio.h>
int main() {
    int number, reverse = 0, remainder;
    printf("Enter an integer: ");
    scanf("%d", &number);
    while (n != 0) {
        remainder = number % 10;
        reverse = reverse * 10 + remainder;
        number /= 10;
    }
    printf("Reversed number is = %d", reverse);
    return 0;
}

Explanation

Before writing a program, we must include standard input output library header file. i.e stdio.h

Ex: #include <stdio.h>

Now in main program, first of all, we have to take 3 variables under int data type.

Now print a message on the screen with "Enter an integer: ", here after, this message print on the screen, user will enter a number belonging to integer data type.

Now we have taken, scanf(); function to read the values of user input. And it is stored in the variable "number".

Now we have to use while loop, where the while loop will run only if the condition satisfied. We have taken "number!=0". i.e. while loop will run if the the number is not equal to 0.

For example, consider we have taken number=54;

In programming we call % as modulus. And from program statement, consider remainder=n%10; which gives remainder of the program.

Here, remainder=54%10; gives remainder 4.

reverse=0*10 + 4

reverse=4;

n /= 10; or n=n/10;

The above statement, n=54/10; it gives value 5.

Now the loop will again run, because now the value of n=5 and not equal to 0.

Now this time, remainder=5%10; which gives the remainder 5.

reverse = 4*10 + 5

reverse = 45;

n/= 10 or n= 5/10

Therefore, n=0;

Now the loop will exit and it prints reverse of the given number. That is 45.

Output:

Enter an integer: 54 Reversed number is = 45{codeBox}

Post a Comment

Previous Post Next Post