6. Write a program to enter 10 no.in an array and replace all
the no: by its reverse. For example if the no.is 57 then it
should be replaced by 75.
Answers
Language:
Python
Program:
a=list(int(input(f"Enter element {i+1}: " )) for i in range(10))
new=list(( i%10)*10+(i//10) for i in a)
print(new)
Output:
Enter element 1: 10
Enter element 2: 11
Enter element 3: 12
Enter element 4: 13
Enter element 5: 14
Enter element 6: 15
Enter element 7: 16
Enter element 8: 14
Enter element 9: 18
Enter element 10: 78
[1, 11, 21, 31, 41, 51, 61, 41, 81, 87]
Logic:
- For a number in form ab which is expanded as a*10 + b use appropriate operators (// and %) number ab to get quotient (here a) and remainder (here b) respectively.
- Once you have a and b separated to join them back in use remainder*10 + quotient to get it reversed as ba.
Expand my program:
I tried to make the program as short as possible here's the explanation if you wanna expand it.
- Make 2 empty lists, one to save input 10 numbers and other to save new reversed numbers.
- Get the user input in list one using append method and for loop.
- Use the logic i talked about above to reverse each number from the list one by one and save the numbers in the new list.
- Print the list.
Attachments:
Answer:
My program is based on c++.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[10], i, r=0
cout<<"Enter the elements:-\n";
for(i=0; i<10; i++)
{
cin>>a[i];
}
for(i=0; i<10; i++)
{
r=(r*10)+a[i];
a[i]=r;
}
cout<<"Elements of the array after reverse:\n";
for(i=0; i<10; i++)
{
cout<<a[i];
}
getch();
}
Explanation:
HOPE MY ANSWER HELPS!!!