Concept explainers
Write a
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)
- Write a program sequence.cc that prompts the user to enter the value of x, the pro- gram outputs the integer k such that ak = 1, the numbers a0, a1, a2, ..., ak, the largest number of the sequence a0, a1, a2, ..., ak, and the position of the largest number in the sequence.arrow_forward(B) Write a program that reads from the user the values of x and h to implement the equation below. -10x + 2h, √x -h, fx = x < 3 x ≥ 3arrow_forwardWrite a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.arrow_forward
- Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. For division, if the denominator is zero, output an appropriate message. Limit the supported operations to + -/ *and write an error message if the operator is not one of the supported operations. Here is some example output: 3 + 4 = 7 13 * 5 = 65 C++arrow_forwardWrite a program which prompts the user for a number and prints the numbers from 1 up to that number, one per line, and then prints out "That's all!". Some of the code has been provided for you. For example, if the user enters 3 the program should print:arrow_forwardOn a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * rn, where n is the distance (number of keys) from that key, and r is 2(1/12). Given an initial key frequency, output that frequency and the next 4 higher key frequencies. Output each floating-point value with two digits after the decimal point, which can be achieved by executingcout << fixed << setprecision(2); once before all other cout statements. Ex: If the input is: 440.0 (which is the A key near the middle of a piano keyboard), the output is: 440.00 466.16 493.88 523.25 554.37 Note: Use one statement to compute r = 2(1/12) using the pow function (remember to include the cmath library). Then use that r in subsequent statements that use the formula fn = f0 * rn with n being 1, 2, 3, and finally 4.arrow_forward
- On a piano, a key has a frequency, say f0. Each higher key (black or white) has a frequency of f0 * rn, where n is the distance (number of keys) from that key, and r is 2(1/12). Given an initial key frequency, output that frequency and the next 4 higher key frequencies. Output each floating-point value with two digits after the decimal point, which can be achieved by executingcout << fixed << setprecision(2); once before all other cout statements. Ex: If the input is: 440.0 (which is the A key near the middle of a piano keyboard), the output is: 440.00 466.16 493.88 523.25 554.37 Note: Use one statement to compute r = 2(1/12) using the pow function (remember to include the cmath library). Then use that r in subsequent statements that use the formula fn = f0 * rn with n being 1, 2, 3, and finally 4. I am having trouble putting them on one sentence line.arrow_forwardWrite a program that accepts a number entered by the user and validates if it is odd oreven. If it is even, print a series up to 4 using range() and if it’s odd print a series up to 7.arrow_forwardwrite a program that takes in an integer in the range 20 to 98 is input the output is a countdown starting from the integer and stopping when both output digits are identicalarrow_forward
- Write a program that continues accepting user inputs as float numbers until the input is an empty string (i.e., a user press Enter without any typing as the input), and then the program will print the largest number among the inputs. If no input is given, print an empty line.arrow_forwardWrite a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH yeHarrow_forwardSolve in Python: Write a program that creates a login name for a user, given the user's first name, last name, and a four-digit integer as input. Output the login name, which is made up of the first five letters of the last name, followed by the first letter of the first name, and then the last two digits of the number (use the % operator). If the last name has less than five letters, then use all letters of the last name. Ex: If the input is: Michael Jordan 1991 the output is: Your login name: JordaM91 Ex: If the input is: Kanye West 2024 the output is: Your login name: WestK24arrow_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