Complete the program which asks the user to enter a 4-digit integer in the range
ID: 3628777 • Letter: C
Question
Complete the program which asks the user to enter a 4-digitinteger in the range [1, 9999]. The program should then display the number with the digits reversed. For example, if the input
number was 3418 then the program would display the number 8143. As another example, if the input number was 406 then the
program would display the number 6040 (there is an implicit leading 0 in front of the 4 in the number 406, i.e., we treat it as
0406). Hint: This can be done using only the integer division / and modulus % operators.
[001] #include <iostream>
[002] using namespace std;
[003]
[004] int main() {
[005] int n;
[006] cout << "Enter an integer [1-9999]: "; cin >> n;
[007] // put the required output statements here.
[008] return 0;
[009] }
Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int n;
cout << "Enter an integer [1-9999]: "; cin >> n;
int k=0;
while(n)
{
int p = n%10;
k = 10*k+p;
n = n/10;
}
cout << k << endl;
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.