I am looking for the C
program for reverse the digits like below:
If i enter:
123456
Then the result would
Here is a simple solution to this complex problem:
#include <stdio.h>
int main()
{
int ch;
ch = getchar();
if (ch != '\n') {
main();
printf("%c", ch);
}
}
#include <stdio.h>
#include <stdlib.h>
static void newline(void)
{
printf("\n");
}
int main()
{
int ch;
ch = getchar();
if (ch != '\n') {
main();
printf("%c", ch);
} else {
atexit(newline);
}
}
If your professor is grading you on performance, then try ggf31416's solution to this problem in another question:
int FastReverse(int num) {
int res = 0;
int q = (int)((214748365L * num) >> 31);
int rm = num - 10 * q;
num = q;
if (rm == 0) return -1;
res = res * 10 + rm;
while (num > 0) {
q = (int)((214748365L * num) >> 31);
rm = num - 10 * q;
num = q;
res = res * 10 + rm;
}
return res;
}
When it absolutely, positively has to be done this nanosecond.