#include <iostream>
using namespace std;
int main() {
int num, reversedNum = 0, remainder;
cout << "Enter an integer: ";
cin >> num;
while (num != 0) {
remainder = num % 10; // extract the last digit of the number
reversedNum = reversedNum * 10 + remainder;// append the digit to the reversed number
num /= 10; // remove the last digit from the original number
}
cout << "The reversed number is: " << reversedNum << endl;
return 0;
}
//===========
OUTPUT:
Enter an integer: 12345
The reversed number is: 54321
Enter an integer: 987654321
The reversed number is: 123456789
Enter an integer: 100
The reversed number is: 1
Explaining :
The program starts by declaring three integer variables: num, reversedNum, and remainder. num is the original number input by the user, reversedNum is the number that will be produced by reversing the digits of num, and remainder is used to extract the last digit of num in each iteration of the while loop.
The program then prompts the user to enter an integer using the cout and cin statements.
The while loop starts by checking whether num is not equal to 0. If num is not 0, the remainder variable is set to the last digit of num using the modulo operator %. The reversedNum variable is then updated by appending the remainder digit to the end of the current value of reversedNum. This is achieved by multiplying reversedNum by 10 and adding the remainder.
Finally, the num variable is updated by dividing it by 10, which effectively removes the last digit of num. The loop then repeats until num becomes 0.
Once the while loop completes, the program prints the reversedNum variable using the cout statement.
Notes:
- * The program is written in C++, which is a high-level programming language commonly used for developing applications, games, and system software.
- * The program uses the
iostreamlibrary, which provides input and output capabilities. This allows the program to interact with the user by prompting for input and displaying output. * The program declares three integer variables: - * The program uses the
coutstatement to display the prompt message "Enter an integer: " to the user. Thecinstatement is then used to read the user's input and store it in thenumvariable. - * The program uses a
whileloop to extract each digit ofnumand append it toreversedNum. The loop uses the modulo operator%to extract the last digit ofnum, and then dividesnumby 10 to remove the last digit. The loop continues untilnumbecomes 0. - * The program updates the
reversedNumvariable by multiplying it by 10 and adding theremainderdigit in each iteration of thewhileloop. - * The program uses the
coutstatement to display the reversed number to the user. - * The program uses the
returnstatement to indicate that the program has successfully completed execution. - * It is important to note that the program assumes that the user will input a valid integer, and does not handle cases where the user enters non-integer values or values that are too large to be stored in an
intvariable. - * The program demonstrates basic programming concepts such as variables, data types, input/output, loops, and basic arithmetic operations.
num, reversedNum, and remainder. Variables are used to store data in memory and can be manipulated using various operations.
No comments:
Post a Comment