I have an example in my textbook with following code and input /* A program that takes a four digits integer from user and shows the digits on the screen separately i.e. if user enters 7531, it displays 7,5,3,1 separately. */
#include <iostream.h>
main()
{
// declare variables
int number, digit;
// prompt the user for input
cout << "Please enter 4-digit number:";
cin >> number;
// get the first digit and display it on screen
digit = number % 10;
cout << "The digits are: ";
cout << digit << ", ";
// get the remaining three digits number
number = number / 10;
// get the next digit and display it
digit = number % 10;
cout << digit << ", ";
// get the remaining two digits number
number = number / 10;
// get the next digit and display it
digit = number % 10;
cout << digit << ", ";
// get the remaining one digit number
number = number / 10;
// get the next digit and display it
digit = number % 10;
cout << digit;
}
A sample output of the above program is given below. Please enter 4-digit number: 5678 The digits are: 8, 7, 6, 5
How can i solve it with loop method.