PROBLEM LINK: CodeChef: Practical coding for everyone
Author: Setter’s name
Tester: Tester’s name
Editorialist: Editorialist’s name
DIFFICULTY : EASY
PREREQUISITES:
Nill
PROBLEM:
Hermione Granger has been working on a new spell which she picked up from the library. This is how the spell works: it completely reverses whatever number she wants. Guide Hermione through this.
QUICK EXPLANATION:
The individual digits are obtained and the positions are reversed
EXPLANATION:
This program takes an integer input from the user. Then the while loop is used until n != 0 is false (0).
In each iteration of the loop, the remainder when n is divided by 10 is calculated and the value of n is reduced by 10 times.
Inside the loop, the reversed number is computed using:
reversedNumber = reversedNumber*10 + remainder
SOLUTIONS:
Setter's Solution
using namespace std;
int main() {
int n, reversedNumber = 0, remainder;
cin >> n;
while(n != 0) {
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout << reversedNumber;
return 0;
}
Tester's Solution
Same Person
Editorialist's Solution
Same Person