Write a program that reads an integer and then calculates and displays its reverse.
Example:
Entry: 19740
Display: 4791
Difficulty level
Video recording
This exercise is mostly suitable for students
#include <stdio.h>
#include <conio.h>
#include <limits.h>
void main()
{
unsigned int NB;
unsigned int REV;
do
{
printf("Enter a positif integer (%u) : ", UINT_MAX );
scanf("%u", &NB);
}while (NB<0 || NB >UINT_MAX);
REV=0;
while(NB>0)
{
REV *= 10;
REV += NB%10;
NB /= 10;
}
/* or : */
/* for (REV=0 ; NB>0 ; NB/=10)
REV = REV*10 + NB%10;
*/
printf("Reverse number : %u\n", REV);
getch();
}
Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Binary trees insertion