div_t is an structure and cause more memory usage you can use % and / operators instead.also you can use while(1) with break for this code.
#include <stdlib.h> #include <math.h> int reverse_int(int int_reverse) { int o = 0; while (1) { o += ((int)int_reverse % 10); if (int_reverse /= 10) o *= 10; else break; } return o; }
- #include <stdlib.h>
- #include <math.h>
- int reverse_int(int int_reverse) {
div_t div_x = div(int_reverse, 10);int reverse_int = div_x.rem;while (div_x.quot) {div_x = div(div_x.quot, 10);reverse_int *= 10;reverse_int += div_x.rem;}return reverse_int;- int o = 0;
- while (1) {
- o += ((int)int_reverse % 10);
- if (int_reverse /= 10)
- o *= 10;
- else break;
- }
- return o;
- }
this is faster cause n===1 || n===2 has two steps and n<=1 has one