<p>Write an appropriate algorithms and program which inputs a positive number fr
ID: 3630932 • Letter: #
Question
<p>Write an appropriate algorithms and program which inputs a positive number from the user. It then outputs each digit of that number in reverse order. Example:<br /><br />Input: 4821<br />Output: 1284<br />Input: 10<br />Output: 01<br />Input: 9<br />Output: 9</p>Explanation / Answer
Algorithm: Input: num (1) Initialize rev_num = 0 (2) Loop while num > 0 (a) Multiply rev_num by 10 and add remainder of num divide by 10 to rev_num rev_num = rev_num*10 + num%10; (b) Divide num by 10 (3) Return rev_num C program code: #include /* Iterative function to reverse digits of num*/ int reversDigits(int num) { int rev_num = 0; while(num > 0) { rev_num = rev_num*10 + num%10; num = num/10; } return rev_num; } /*Driver program to test reversDigits*/ int main() { int num = 4562; printf("Reverse of no. is %d", reversDigits(num)); getchar(); return 0; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.