C Program to reverse Five digit number

In this article, I will show you how you can write a C Program to reverse Five digit number. I will explain the algorithm, concepts and then help you to write C Program to reverse Five digit number.

1. Program Outline

In this program, you’ll have to take a five digit number as input from user and then reverse the number and then display reversed number in terminal.

2. Concepts

To write this C program to reverse Five digit number, you need to learn the following concepts of C Programming:

  1. Input
  2. Variables
  3. Loop

3. Algorithm

To write this C program, follow this Algorithm:

Step 1: START
Step 2: Input num
Step 3: Initialize revNum = 0
Step 4: Loop while num > 0
Step 5: Multiply revNum by 10 and add remainder of num  
Step 6: divide by 10 to revNum
Step 7: revNum = revNum*10 + num%10;
Step 8: Divide num by 10
Step 9: Return revNum
Step 10: STOP

4. Program

Here’s the C Program to reverse Five digit number:

#include <stdio.h>
 
int revNumber(int num)
{
    int revNum = 0;
    while (num > 0) {
        revNum = revNum * 10 + num % 10;
        num = num / 10;
    }
    return revNum;
}
/* codewin.org */
int main()
{
    int num = 4562;
    printf("Reversed number: %d", revNumber(num));
 
    getchar();
    return 0;
}

5. Output

Here’s the output of the above C Program:

C Program to reverse Five digit number
groot
groot

Leave a Reply

Your email address will not be published. Required fields are marked *