How can I find digits of elements of array in reverse? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How can I find digits of elements of array in reverse?

Write a recursive C function digit_reverse(.) that takes a positive integer (up to six digits long) as argument, and returns another integer where the digits are reversed. For example, if the number passed as argument is 2371, the value returned will be 1732. Write a main function that uses "digit_reverse" to determine, if a given number is a palindrome. A palindrome is one which reads the same forward or backward (like 120021, 234, 5, etc.). Read a list of positive numbers in an array and print them after reversing their digits. Further report the palindromes and the number of palindromes.

17th Jan 2022, 4:43 PM
Nitu Rao
5 Answers
+ 3
You can get a last digit for a number by num%10 so that digit will be first digit of reverse number.., you can add further digits by rev*10 + num%10.. Ex: 234 234%10=4, rev=0*10+4=4, num=234/10=23 23%10=3, 4*10+3=43, 23/10=2 2%10=2, 43*10+2=432, 2/10=0 Now you can end loop.. Hope it helps...
17th Jan 2022, 4:55 PM
Jayakrishna 🇮🇳
+ 1
#include<stdio.h> #include<math.h> int digit_reverse(int A); int main() { int n,i,A[10],reversed[10]; printf("Enter n:\n"); scanf("%d",&n); printf("%d\n",n); printf("The input array is :\n"); for (i=0;i<n;i++) scanf("%d",&A[i]); for (i=0;i<n;i++) printf("%d\n",A[i]); for (i=0;i<n;i++) { reversed[i]=digit_reverse(A[i]); //you are passing single number //} printf("Reverse number=%d\n",reversed[i]); } //added } //no need of using array in function.. //corrected way.. int digit_reverse( int A) { int reversed=0; while ( A>0 ) { reversed=reversed*10+A%10; A/=10; } return reversed; }
17th Jan 2022, 5:59 PM
Jayakrishna 🇮🇳
0
#include<stdio.h> #include<math.h> int digit_reverse(int A[]); int main () { int n,i,A[10],reversed[10]; printf("Enter n:\n"); scanf("%d",n); printf("%d\n",n); printf("The input array is :\n"); for (i=0;i<n;i++) scanf("%d",&A[i]); for (i=0;i<n;i++) printf("%d\n",A[i]); for (i=0;i<n;i++) { reversed[i]=digit_reverse(A[i]); } printf("Reverse number=%d\n",reversed[i]); } int digit_reverse( int A[]) { int reversed[10],i; while (A[i]>0) { reversed[i]=reversed[i]*10+A[i]%10; A[i]/=10; } return reversed[i]; }
17th Jan 2022, 5:31 PM
Nitu Rao
0
This is my code.It is wrong.Can you tell me what I have to change in this code so that it gives reversed digits array.
17th Jan 2022, 5:33 PM
Nitu Rao
0
Got it,Thank you
18th Jan 2022, 7:05 AM
Nitu Rao