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

Expert's answer

#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;
}
LATEST TUTORIALS
APPROVED BY CLIENTS