Write a
nonnegative integer n in reverse order.
Input. One nonnegative 64-bit
integer.
Output. Print the number in reverse order.
Sample input 1 |
Sample output 1 |
1234 |
4321 |
|
|
Sample input 2 |
Sample output 2 |
100 |
001 |
loops
Divide the number
n by 10 until we get 0. At each iteration print the last digit of the
current number.
Read the input value of n.
scanf("%lld", &n);
If n = 0, print 0.
if (n == 0) printf("0");
Print the
digits of number n in the reverse order.
while(n > 0)
{
printf("%d",n%10);
n /= 10;
}
printf("\n");
Algorithm realization – reverse
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char
s[100];
int
main(void)
{
gets(s);
reverse(s,s+strlen(s));
puts(s);
return 0;
}