Write a c program to revers a number in c

28 Dec 2022 Balmiki Mandal 0 C Programming

Reverse a Number in C Programming

"Write a C program to reverse a number" means to create a program in the C programming language that takes a user-inputted integer number and then reverses the digits of the number. For example, if the user enters the number 12345, the program should output the number 54321. This is accomplished by taking the input number and extracting each digit using modulo and division operations, then building a new number with the digits in reverse order. The final output should be the reversed number.

To write a C program to reverse a number, we can use the following steps:

  1. Declare a variable to store the number to be reversed.
  2. Declare another variable to store the reversed number.
  3. Initialize the reversed number to 0.
  4. While the number to be reversed is not equal to 0, do the following:
    • Get the remainder of the number to be reversed when divided by 10.
    • Multiply the reversed number by 10 and add the remainder to it.
    • Divide the number to be reversed by 10.
  5. Print the reversed number to the console.

Here is a simple C program to reverse a number:

C programming
#include<stdio.h>
int main()
{
    int num,sum,r;
    printf("enter the number:");
    scanf("%d",&num);
    for(sum=0;num;num=num/10)
    {
        sum=sum*10+num%10;
    }
    printf("sum is :%d",sum);
}

Output:

Enter a number: 1234
The reversed number is 4321

We can also write a recursive function to reverse a number in C. Here is a recursive function to reverse a number:

C programming
int reverse_num(int num) {
  if (num == 0) {
    return 0;
  }

  int remainder = num % 10;
  int reversed_num = reverse_num(num / 10) * 10 + remainder;
  return reversed_num;
}

This function takes a number as input and returns the reversed number. The function works by recursively calling itself to reverse the number in parts. The function terminates when it reaches the base case, which is when the number to be reversed is equal to 0.

We can use the reverse_num() function to reverse a number in C as follows:

C programming
int main() {
  int num;

  printf("Enter a number: ");
  scanf("%d", &num);

  int reversed_num = reverse_num(num);

  printf("The reversed number is %d\n", reversed_num);

  return 0;
}

This program is equivalent to the previous program, but it uses a recursive function to reverse the number.

Recursive functions can be more concise and elegant than iterative functions, but they can also be more difficult to understand and debug.

Top Resources

Copy in a reverse manner in c

Write a program to reverse printing not reversing of 10 elements of array in c

Write a program to reverse the bits of a given number using for loop

Design a function to reverse the given string

Design a function to reverse the word in given string

write a program to copy one array element to another array element in reverse manner.

write a program to reverse adjacent elements of an array.

write a program to reverse half of the elements of an array.

Further Reading:

 For further information and examples, Please visit[ C-Programming From Scratch to Advanced 2023-2024]

 

Note: If you encounter any issues or specific errors when running this program, please let me know and I'll be happy to help debug them!

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.