-5

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.

rakibulmuhajir
  • 61
  • 2
  • 11

4 Answers4

2

A loop executes the same section of code zero or more times, depending on the loop's conditional.

If you carefully examine your code, you see that you are repeating essentially the same code, four times: calculate the remainder of a division by ten, integer division by ten, and some output.

So, it seems logical to simply write a loop that executes the same exact code, which only executes these operations one, but have the loop execute this section of code four times.

And that's how you do it.

Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
1

First you need to recognise part of code which repeat itself more times and that code put inside some loop(for,while,do-while) and declare how many times it will execute,mostly it is some bool expresion. In you case this part of code you need to put inside for,while or do-while loop:

digit = number % 10;
cout << digit << ", ";
number = number / 10;

for example:

for(int i=0;i<4;i++){
    digit = number % 10;
    if(i!=3)
        cout << digit << ", ";
    else
        cout << digit;

    number = number / 10;
}
1

To get an equivalent output You can use for example do-while loop

cout << "The digits are: ";

do
{
    cout << number % 10;
} while ( ( number /= 10 ) && cout << ", " );

Or

do
{
    cout << number % 10;

    number /= 10;

    if ( number ) cout << ", ";
} while ( number );

If you want to output exactly four digits provided that the number indeed has four digits then you can write

int i = 0;
do
{
    cout << number % 10;

    number /= 10;

    if ( number ) cout << ", ";
} while ( ++i < 4 && number );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Is there any specific reason you need to perform division on an integer? If you accept the user's input as a string it is possible to simply iterate over the characters contained e.g

string val;
cin >> val;
int len = val.length();

for(int i = len-1; i >= 0; --i){
  cout << val[i]<<",";
}

Of course if you know the number of digits beforehand you can easily do this using an int

int val;
cin >> val;

for(int i = 0; i < 4; ++i){
  int digit = val % 10;
  val /= 10;
  cout << digit << ",";
}

If you need to extend the solution to cover any arbitrary length int you would need a while loop with this condition

while(val > 0){
      int digit = val % 10;
      val /= 10;
      cout << digit << ",";
}
Lloyd Crawley
  • 606
  • 3
  • 13