Computer Science, asked by DrNeedHelp, 1 month ago

Write a Program that takes a number and check if the given number is a 3 digit
number or not. (Compulsory to use loops such as for, while)

Answers

Answered by Equestriadash
1

The following co‎des have been written using Python.

\tt n\ =\ in put("Enter\ a\ number:\ ")\\c\ =\ 0\\for\ i\ in\ n:\\{\ \ \ \ \ }c\ =\ c\ +\ 1\\if\ c\ ==\ 3:\\{\ \ \ \ \ }print(n,\ "is\ a\ 3-digit\ number.")\\else:\\{\ \ \ \ \ }print(n,\ "is\ not\ a\ 3-digit\ number.")

Once we receive the input, we assign a variable to represent the number of characters/digits of the number. We start a loop to calculate the same. The travsersing variable 'i', goes through all the characters in 'n', and adds them to the variable representing the number of digits. We use a conditional statement to check if 'c' is equivalent to 3 or not. Appropriate print statements have been given for both cases.

NOTE: Since no actual calculation(s) are being performed, the input has been received in the form of a string value, instead of an integer value. If we were to use \tt int(), the program would result in an error once the loop starts. In case you'd still like to receive the input in the form of an integer value, appropriate modifications have been made and shown below.

\tt n\ =\ int(in put("Enter\ a\ number:\ "))\\c\ =\ 0\\for\ i\ in\ str(n):\\{\ \ \ \ \ }c\ =\ c\ +\ 1\\if\ c\ ==\ 3:\\{\ \ \ \ \ }print(n,\ "is\ a\ 3-digit\ number.")\\else:\\{\ \ \ \ \ }print(n,\ "is\ not\ a\ 3-digit\ number.")


Equestriadash: Thanks for the Brainliest! ^_^"
Similar questions