Question

In: Computer Science

Write a program that calculates how many digits an integer value acquired via scanf() contains. A...

Write a program that calculates how many digits an integer value acquired via scanf() contains. A correctly functioning program should produce the following output, where the user entered the value 374:

Enter a number:  374
The number 374 has 3 digits


You may assume that the input integer has no more than four digits. Use if statements to test the number. For example, if the integer value entered by the user is between 0 and 9, it has one digit. If the integer value is between 10 and 99, it has two digits.

Rewrite the program without using if or switch statements. Instead use a do-while loop.

When implemented correctly, the user should be able to input any integer value (that will fit in a storage variable of type int) and its number of digits will be correctly determined without you needing to implement special logic for each possible quantity digits.

Hint:
The number of digits is very simply related to the number of times the input integer value may be divided by 10.

Solutions

Expert Solution

As you didin't mentioned the programming language i did it in python

------------------------------------------------------------------------

a = int(input("Enter a number: ")) #enter the number you want to know the digits
b = str(a) #convert the number to string
c = len(b) #find the length of the string
print("The number %d has %d digits"%(a,c)) #print the digits in the number
-----------------------------------------------------------------------

------------------------------------------------------------------------

Kindly comment your queries if any,upvote if you like it. Thank You!!


Related Solutions

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.
Write a java program that asks the user for a positive integer N and then calculates...
Write a java program that asks the user for a positive integer N and then calculates a new value for N based on whether it is even or odd: if N is even, the new N is N/2. (Use integer division.) if N is odd, the new N is 3*N + 1. Repeat with each new N until you reach the value 1. For example, say the initial value is 12. Then the successive values are: 12 (even, next value...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
Write a java program that uses a stack to reverse an integer of three digits. The...
Write a java program that uses a stack to reverse an integer of three digits. The program must prompt the user to input an integer of 3 digits and display it in reverse. - Your program must include the Stack class, the Reverse class, and the Test class. - In the Test class, the program must prompt the user to input a 3-digit number and display it in reverse. - Class Reverse must use the Stack class to reverse the...
4. Write a program to compute the sum of digits of an integer. For example, given...
4. Write a program to compute the sum of digits of an integer. For example, given the integer 35931, your program should output 21. (21 is the sum 3 + 5 + 9 + 3 + 1) PYTHON
1. Write the C++ code for a program that calculates how many days are left until...
1. Write the C++ code for a program that calculates how many days are left until Halloween, when given as an input how many weeks are left until Halloween. Use variables named weeks and days. ------------ 2. What header file must be included - To perform mathematical functions like sqrt? - To use cin and cout? - To use stream manipulators like setprecision? -------------------- 3. What value will be stored in the variable t after each of the following statements...
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
please write it in printf and scanf please Please write it in c# program please and...
please write it in printf and scanf please Please write it in c# program please and if possible no arrays only loops Loop Introduction Assignment Using the conditions below, write one program that calculates a person’s BMI. Your main() function will call functions 1, 2, and 3. Your program will contain three functions: Function #1: Will ask the user for their weight in pounds and their height in inches. Your function will convert the weight and height into Body Mass...
Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has.
Use JAVAProgram #1: Hotel Occupancy: Write a program that calculates the occupancy rate for a hotel. The program should start by asking the user how many floors the hotel has. A for loop should then iterate once for each floor. In each iteration of the for loop, the program should ask the user for the number of rooms of the floor and how many of them are occupied. After all of the iterations are complete the program should display how...
Write a C program that reads an integer value. Assume it is the number of a...
Write a C program that reads an integer value. Assume it is the number of a month of the year; print out the name of that month (Hint: months need to be captured in an array).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT