Concept explainers
Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display
n
Remember to display “zero” if the user types in just a 0. (Note: This exercise is a hard one!)
Program Plan:
- Include the required files
- Define the main function
- Declare the variable “num1”, “num2”, “rt_digit”, “val”.
- Get the number from the user.
- Check whether “num1” not equal to “0”.
- Find the remainder using mod operator.
- Divide the “number” by “10”.
- Multiply “val” to “num” and add with the “rt_digit”.
- Condition for while loop to check whether the “val” not equals to “10”.
- Multiply the “val” with “10”.
- Assign the “rt_digit” to “0”.
- Do until the condition fails.
- Find the remainder using mod operator.
- Check whether the “rt_digit” equals to “1”.
- Print the value “one”.
- Check whether the “rt_digit” equals to “2”.
- Print the value “two”.
- Check whether the “rt_digit” equals to “3”.
- Print the value “three”.
- Check whether the “rt_digit” equals to “4”.
- Print the value “four”.
- Check whether the “rt_digit” equals to “5”.
- Print the value “five”.
- Check whether the “rt_digit” equals to “6”.
- Print the value “six”.
- Check whether the “rt_digit” equals to “7”.
- Print the value “seven”.
- Check whether the “rt_digit” equals to “8”.
- Print the value “eight”.
- Check whether the “rt_digit” equals to “9”.
- Print the value “nine”.
- While loop to check the condition whether the “num2” not equal to “0”.
Program to display the numbers to their equivalent words.
Explanation of Solution
Program:
//Include required header files
#include <stdio.h>
//Main function
int main()
{
//Declare the required variable
int num1 = 0, rt_digit = 0, num2 = 0, val = 1;
//Get the number from the user
printf("\nEnter a number: ");
scanf("%i", &num1);
//While loop to check the condition
while (num1 != 0)
{
//Find the remainder using mod operator
rt_digit = num1 % 10;
//Divide the "num1" by "10"
num1 = num1 / 10;
//Multiply "val" to "num" and add with the "rt_digit"
num2 = num2 * val + rt_digit;
//While loop to check the condition
while (val != 10)
{
//Multiply "val" with "10"
val = val * 10;
}
}
//Assign the "rt_digit" to "0"
rt_digit = 0;
//Do until the condition fails
do {
//Find the remainder using mod operator
rt_digit = num2 % 10;
//Check whether the "rt_digit" equals to "0"
if (rt_digit == 0)
{
//Print the value zero
printf("zero ");
}
//Check whether the "rt_digit" equals to "1"
else if (rt_digit == 1)
{
//Print the value one
printf("one ");
}
//Check whether the "rt_digit" equals to "2"
else if (rt_digit == 2)
{
//Print the value two
printf("two ");
}
//Check whether the "rt_digit" equals to "3"
else if (rt_digit == 3)
{
//Print the value three
printf("three ");
}
//Check whether the "rt_digit" equals to "4"
else if (rt_digit == 4)
{
//Print the value four
printf("four ");
}
//Check whether the "rt_digit" equals to "5"
else if (rt_digit == 5)
{
//Print the value five
printf("five ");
}
//Check whether the "rt_digit" equals to "6"
else if (rt_digit == 6)
{
//Print the value six
printf("six ");
}
//Check whether the "rt_digit" equals to "7"
else if (rt_digit == 7)
{
//Print the value seven
printf("seven ");
}
//Check whether the "rt_digit" equals to "8"
else if (rt_digit == 8)
{
//Print the value eight
printf("eight ");
}
//Check whether the "rt_digit" equals to "9"
else
//Print the value nine
printf("nine ");
//Divide the "num2" by "10"
num2 = num2 / 10;
//While loop to check the condition
} while (num2 != 0);
//Return the value 0
return 0;
}
Enter a number: 56
five six
Want to see more full solutions like this?
Chapter 5 Solutions
Programming in C
Additional Engineering Textbook Solutions
Computer Systems: A Programmer's Perspective (3rd Edition)
Starting Out with Python (3rd Edition)
Starting Out With Visual Basic (7th Edition)
Introduction To Programming Using Visual Basic (11th Edition)
Modern Database Management (12th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
- Using SPIM, write and test a program that reads in a positive integer using the SPIM system calls. If the integer is not positive, the program should terminate with the message “Invalid Entry”; otherwise the program should print out the names of the digits of the integers, delimited by exactly one space. For example, if the user entered “728,” the output would be “Seven Two Eight.”arrow_forwardWrite a program that reads a four-digit integer, such as 2014, and then displays it, one digit per line in reverse order, like so: 4 1 0 2 Your prompt should tell the user to enter a four-digit integer. You can then assume that the user follows directions. (Hint: Use the division andm remainder operators.)arrow_forwardWrite a program that reads postfix expressions and prints their values. Each input expression should be entered on its own line, and the program should terminate when the user enters a blank line. Assume only binary operators, and that the expressions contain no variables. Note that you will need to use parentheses to indicate the order of application of the operators in the expression. Here are sample input–output pairs: 78 78 78 6 + 84 78 6 + 9 2 - / 12arrow_forward
- 6. Write a program that reads in an integer and breaks it into a sequence of individual digits. For example, the input 16384 is displayed as 16384 You may assume that the input has no more than five digits and is not negative.arrow_forwardWrite a program that reads an integer and determines and prints whetherit’s odd or even. [Hint: Use the remainder operator. An even number is a multiple of two. Any multiple of two leaves a remainder of zero when divided by 2.]arrow_forwardWrite a program that reads from the user a character (*q' or 'c'). If the character is 'c', the program reads from the user the radius r of a circle and prints its area. If the user enters 'q', the program asks the user to enter the length and width of a quadrilateral. We assume the quadrilateral is either a square or rectangle. You should print if the quadrilateral is square or rectangle. Otherwise, it prints "Wrong character" PS: Use the following formulas : area of circle=3.14*r? Iarrow_forward
- 6. A palindrome is a number or text phrase that reads the same backwards or forwards. For example, each of the following five-digit integers is a palindrome: 12321, 55555, 45554 and 11611. Write a program that reads a 5 digit integer and determines whether it is a palindrome. Hint: One way to do this is to use division (/) and modulus (%) operators to separate the number into individual digits.arrow_forwardA palindromic number is a number that is the same when written forwards or backwards. The first few palindromic numbers are therefore are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111, 121, 323 Input a number from the user. Write a program that reverses the digits of a number. Use this program to find if the number is a palindrome. Print if the number is palindrome or not.arrow_forwardUse python The speeding ticket fine policy in Wonderland-Bemidji is $50 plus $5 for each mph over the limit, plus an additional penalty of $200 for any speeds over 90mph. Write a program that accepts a speed limit (<= 90) and a clocked speed, and either outputs a message indicating the speed was legal, or prints the amount of the fine if the speed is illegal.arrow_forward
- 1 commercial (): Function that takes an Input argument kwh of type unsigned int and returns the amount due as a value of type double. This function should be used to compute the bill for commercial customers. 3. industrial (): Function that takes two input arguments kwh peak and kwh off peak of type unsigned int both and returns the amount due as a value of type double. This function should be used to compute the bill for Industrial customers. in the test part of your report, specify the results provided by your program for: 1) residential use of 500 kwh, 11) commercial use of 1000 kwh; III) commercial use of 2000 kwh; Iv) Industrial use of 1000 kwh (peak hours) and 2000 kwh (off-peak hours).arrow_forward3. Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example, if an integer is 932, the sum of all its digits is 14. Hint: Use the % operator to extract digits, and use the / operator to remove the extracted digit. For instance, 932 % 10 = 2 and 932 / 10 = 93. A sample run: Enter a number between 0 and 1000: 999 The sum of the digits is 27arrow_forwardWrite a program that calculates and prints the average of several integers (you don't know how many will be entered). Assume the last value read with scanf is the sentinel 9999 (this tells you that you have reached the end of the list of numbers). A typical input sequence might be 10 8 11 7 9 9999 indicated that the average of all the values preceding 9999 is to be calculated. (9999 should not be included in the average) c programmingarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education