Home / Programming / Code Snippets / C Snips / Reverse Number C Program, Number Reversing in C programming

Reverse Number C Program, Number Reversing in C programming

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number

#include <stdio.h>
int main()
{
    int n, reversedNumber = 0, remainder;

    printf("Enter an integer: ");
    scanf("%d", &n);

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

    printf("Reversed Number = %d", reversedNumber);

    return 0;
}
Enter an integer: 2345
Reversed Number = 5432

This program takes an integer input from the user. Then the while loop is used until n != 0is false. In each iteration of while loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by times.

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number


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;
}

 

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number

Reverse number C program using recursion

#include <stdio.h>
 
long reverse(long); 
 
int main()
{
   long n, r;
 
   scanf("%ld", &n);
 
   r = reverse(n);
 
   printf("%ld\n", r);
 
   return 0;
}
 
long reverse(long n) {
   static long r = 0;
 
   if (n == 0) 
      return 0;
 
   r = r * 10;
   r = r + n % 10;
   reverse(n/10);
   return r;
}

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number

#include <stdio.h>
/* Iterative function to reverse digits of num*/
int reversDigits(int num)
{
int rev_num = 0;
while(num > 0)
{
rev_num = rev_num*10 + num%10;
num = num/10;
}
return rev_num;
}
/*Driver program to test reversDigits*/
int main()
{
int num = 4562;
printf("Reverse of no. is %d", reversDigits(num));
getchar();
return 0;
}

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number

Using goto function

#include<stdio.h>

int main() {

int i=0,rev=0,n;

printf("n:");

scanf("%d",&n);

x:if(n>0)

rev=rev*10+n%10;

n=n/10;

if(n>0)

goto x;

printf("%d",rev);

}

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number

Reverse a Number using For Loop in C Programming

Reverse Number C Program, Number Reversing in C programming, Best way to reverse a number

Method 3: C Program Code To Reverse a Number using Function

C Program to Reversing an Array Elements in C Programming

Output :

About revive

Leave a Reply

Leave a Reply

  Subscribe  
Notify of
%d bloggers like this: