Write a C program to reverse the given number




c programming
c programming

C program to reverse a number

/**
C program to reverse a number by codebind.com
*/

#include <stdio.h>

int main() {
  int n, reverse = 0;

  printf("Enter a number to reverse : \n");
  scanf("%d", &n);

  while (n != 0)
  {
    reverse = reverse * 10;
    reverse = reverse + n%10;
    n       = n/10;
  }

  printf("Reverse of entered number is = %d\n", reverse);

  return 0;
}

/*
OUTPUT:
Enter a number to reverse :
12345
Reverse of entered number is = 54321
*/

C program to reverse number using recursion

/**
C program to reverse number using recursion by codebind.com
*/

#include <stdio.h>


long Reverse(long n) {
  static long reversed_number = 0;
  if (n == 0)
    return 0;

  reversed_number = reversed_number * 10;
  reversed_number = reversed_number + n % 10;
  Reverse(n/10);
  return reversed_number;
}

int main()
{
  long number, reversed_result;

  printf("Enter a number to reverse : \n");
  scanf("%ld", &number);

  reversed_result = Reverse(number);

  printf("Reverse of entered number is = %ld\n", reversed_result);

  return 0;
}


/*
OUTPUT:
Enter a number to reverse :
12345
Reverse of entered number is = 54321
*/

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com





Be the first to comment

Leave a Reply

Your email address will not be published.


*