Computer Science, asked by mitradeboytree, 2 months ago

Write a program to input a three digit number and display all the
digits by using arithmetical operators.
Sample Input: 472
Sample Output: 4
7
2
comments of the program ​

Answers

Answered by Equestriadash
3

The following co‎des have been written using Python.

\tt n\ =\ int(in put("Enter\ a\ three\ digit\ number: "))\\if\ n\ >=\ 100\ and\ n\ <=\ 999:\\{\ \ \ \ \ }p\ =\ 2\\{\ \ \ \ \ }while\ p\ >\ -1:\\{\ \ \ \ \ }{\ \ \ \ \ }r\ =\ n//10* *p\\{\ \ \ \ \ }{\ \ \ \ \ }print(r)\\{\ \ \ \ \ }{\ \ \ \ \ }n\ =\ n\%10* *p\\{\ \ \ \ \ }{\ \ \ \ \ }p\ =\ p\ -\ 1

We input the data using the input() function, and call it with the int() function to ensure that the data being input is an integer value. To check if it's a 3-digit number or not, we use a conditional statement. If it results to True, it proceeds with the rest of the program, printing each digit line by line.

Another shorter approach is as follows:

\tt n\ =\ int(in put("Enter\ a\ three\ digit\ number: "))\\while\ n\ !=\ 0:\\{\ \ \ \ \ }r\ =\ n\%10\\{\ \ \ \ \ }print(r)\\{\ \ \ \ \ }n\ =\ n//10\\

This co‎de, however, prints the digits from the unit's place first, onto the hundredth's place.

Similar questions