Answer to Question #283738 in C++ for Collay

Question #283738

3. Place Values

by CodeChum Admin

Manipulating values from a series of numbers is fun, but let's try exploring the use of loops a little more.


How about printing out each digit of a number by place values, in ascending order?



Instructions:

  1. Create a variable and input a random positive integer.
  2. Using while loop, print out each digit of the inputted integer in separate lines, starting from its rightmost digit until the leftmost digit of the number. Tip: Use % 10 to get the rightmost digit, and / 10 to remove it from the number.
  3. Another tip: don't forget to consider the case wherein the input is `0`

Input

A line containing an integer.

214

Output

Multiple lines containing an integer.

4
1
2
1
Expert's answer
2021-12-30T07:14:05-0500
#include <iostream>
using namespace std;
int main(){
    int number;
    cout<<"Input\nA line containing an integer.\n";
    cin>>number;


    cout<<"Output\nMultiple lines containing an integer.\n";
    cout<<number % 10<<endl;
    number /= 10;
    while(number != 0){
        cout<<number % 10<<endl;
        number /= 10;
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog