12. Program that inputs 3-digit number and displays its digits in speprate three lines.

Chapter 4: 

Input And Output

Programming Exercise

Problem # 12:

Write a program that inputs 3-digit number and displays its digits in speprate three lines. For example if the user enters 123, the program displays the output as follows:
    1
    2
    3

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int n, a, b, c;
    cout<<"Enter a 3-digit number : ";
    cin>>n;
    a = n % 10;
    n = n / 10;
    b = n % 10;
    n = n / 10;
    c = n % 10;
    cout<<c<<endl<<b<<endl<<a<<endl;
    return 0;
}

Code Screenshot:

Chapter 4, Coding tutorial, Computer Science, CPP Basics, Exercise Solution, Input and Output, IT Series, Object Oriented Programming using C++, Programming for beginners, Programming Tutorial

Output:

Chapter 4, Coding tutorial, Computer Science, CPP Basics, Exercise Solution, Input and Output, IT Series, Object Oriented Programming using C++, Programming for beginners, Programming Tutorial


Let me know in the comment section if you have any question.

Previous Post:
11. Program that inputs temperature from the user in Farenheit and converts it into Celsius degree.

Comments

Post a Comment