Question

In: Computer Science

Write a Python program that reads an integer and prints how many digits the number has,...

Write a Python program that reads an integer and prints how many digits the number has, by checking whether the number is ≥10,≥100,≥1000, and so on (up to 1,000,000). Your program should also identify if a number is negative.

Solutions

Expert Solution

Python Program that print number of digit and identify if number is negative:

# Prompt for user input here input() function return string
# so we need to convert string to int first by using int() function.
number = int(input("Enter any number: "))

numOfDigit = 0  # Variable to hold number of digit

# first we need to check with higher number(Which is 1000000 in this case)
# then continue checking for lower values.
# Check if number is greater than 1000000
if number >= 1000000:
    numOfDigit = 7
# Check if number is greater than 100000
elif number >= 100000:
    numOfDigit = 6
# Check if number is greater than 10000
elif number >= 10000:
    numOfDigit = 5
# Check if number is greater than 1000
elif number >= 1000:
    numOfDigit = 4

# Check if number is greater than 100
elif number >= 100:
    numOfDigit = 3
# Check if number is greater than 10
elif number >= 10:
    numOfDigit = 1

#  check if number is negative that is less than 0
if number < 0:
    print(number, "is negative")

# if number is greater than 0 then print number of digit.
else:
    print("Number of digit: ", numOfDigit)

Sample Outputs :

Screenshot of Code :


Related Solutions

in .java Write a program that reads an integer with 3 digits and prints each digit...
in .java Write a program that reads an integer with 3 digits and prints each digit per line in reverse order. Hint: consider what you get from these operations: 319%10, 319/10, 31%10, ... Enter an integer of exactly 3 digits(e.g. 538): 319 9 1 3 Hint: consider what you get from these operations: 319%10 319/10 31%10
in.java Write a program that reads an integer from the user and prints a rectangle of...
in.java Write a program that reads an integer from the user and prints a rectangle of starts of width 5 3 and height N. Sample run 1: Enter N: 5 *** *** *** *** *** Bye Sample run 2: Enter N: 8 *** *** *** *** *** *** *** *** Bye Sample run 3: Enter N: 2 *** *** Bye Sample run 4: Enter N: -2 Bye
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
C program, please Write a program that reads a sequence of 10 integer inputs and prints...
C program, please Write a program that reads a sequence of 10 integer inputs and prints the smallest and largest of the inputs and the number of even and odd inputs. for a beginner please, you could use a while loop,if-else,
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a Python program that reads in an amount in cents and prints the dollar amount...
Write a Python program that reads in an amount in cents and prints the dollar amount in that and the remaining value in cents. For example, if the amount reads in is 360 (cents), the program would print 3 dollars and 60 cents. if the amount read in is 75 (cents), the program would print 0 dollars and 75 cents.
Write a short main program that reads an integer n from standard input and prints (to...
Write a short main program that reads an integer n from standard input and prints (to standard output) n lines of * characters. The number of *’s per line should double each time, starting with 1. E.g., if n = 5, the output should be as follows: * ** **** ******** ****************
Write a program called x.c that reads an integer n from standard input, and prints an...
Write a program called x.c that reads an integer n from standard input, and prints an nxn pattern of asterisks and dashes in the shape of an "X". You can assume n is odd and >= 5. Solve this problem using only while loop. Solution: ./x Enter size: 5 *---* -*-*- --*-- -*-*- *---* ./x Enter size: 9 *-------* -*-----*- --*---*-- ---*-*--- ----*---- ---*-*--- --*---*-- -*-----*- *-------* ./x Enter size: 15 *-------------* -*-----------*- --*---------*-- ---*-------*--- ----*-----*---- -----*---*----- ------*-*------ -------*------- ------*-*------...
Write a program that reads a positive integer n , prints all sums from 1 to...
Write a program that reads a positive integer n , prints all sums from 1 to any integer m 1≤m≤n . For example, if n=100, the output of your program should be The sum of positive integers from 1 to 1 is 1;       The sum of positive integers from 1 to 2 is 3;       The sum of positive integers from 1 to 3 is 6;       The sum of positive integers from 1 to 4 is 10;      ...
Write a program called distance_square.c that reads an integer n from standard input, and prints an...
Write a program called distance_square.c that reads an integer n from standard input, and prints an nxn pattern of integers. Each integer is the minimum number of steps required to reach the centre of the square. Steps can only be up, down, left or right (no diagonal movement). the question should be allowed to use only while loop 4 3 2 3 4 3 2 1 2 3 2 1 0 1 2 3 2 1 2 3 4 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT