C and C++ Programs to Reverse a Number

This post includes a C program and a C++ program to reverse a given number. That is, to reverse the order of digits. If 543 is entered, it will be displayed as 345.

C Program:

#include<stdio.h>
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);
}

C++ Program:

#include<iostream.h>
void main()
{
int n, reverse = 0;
cout<<"Enter a number to reverse:\n";
cin>>n;
while (n != 0)
    {
    reverse = reverse * 10;
    reverse = reverse + n%10;
n = n/10;
    }
cout<<"Reverse of entered number is ="<<reverse;
}


No comments :

Post a Comment