In this practical we will learn to reverse any given number in C & C++.

If Input: 1357

Then, Output: 7531

C program to reverse a given number

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

 

C++ program to reverse a given number

#include <iostream>
using namespace std;

int main() {
    int n, reverse = 0;
    
    cout << "Enter an integer: ";
    cin >> n;
    while(n != 0) {
        int remainder = n%10;
        reverse = reverse*10 + remainder;
        n/=10;
    }
    
    cout << "Reversed number = " << reverse;
    
    return 0;
}

Check out – All CSE Basic Practicals

Thanks for visiting us. Do share us among your friends.

C/C++ program to reverse a given number – Computer Practical
Tagged on:     

Leave a Reply

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