Question

In: Computer Science

Write a while loop to ask the user to type number of hours(double) they work per...

Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input.

If number of hours is greater than 0 and less than 5, then:  salary = numberofhours * 12, loop continues, the user can type another number

If number of hours is greater or equal to 5, and less than 12, then: salary = numberofours * 14, loop continues, the user can type another number

If number of hours is greater or equal to 12, and less than 24, then salary = numberofours * 16, loop continues, the user can type another number

If number of hours is negative or greater than 24, then the loop stops, message "Invalid Input" will be printed.

Solutions

Expert Solution

Dear student,

Let us code the above-asked loop using C programming language as no specific language is mentioned. We can use any language but the logic will remain the same. We are assuming 0 is included in the 0 to 5 hours segment as there is nothing specified for it.

The while loop is in Bold.

#include <stdio.h>

int main()
{
double hours=1.0;// any value just to enter the loop for the first time

double sal=0.0;

while(hours>=0 && hours<24)

{

printf("\nEnter the number of hours you work per day: ");

scanf("%lf", &hours);

if(hours>=0 && hours<5)

{

sal=hours*12;

printf("\nYour salary is: %lf",sal);

}

else if(hours>=5 && hours<12)

{

sal=hours*14;

printf("\nYour salary is: %lf",sal);

}

else if(hours>=12 && hours<24)

{

sal=hours*16;

printf("\nYour salary is: %lf",sal);

}

else

{

printf("\nInvalid Input");

break; //this command ends the loop

}

}//while loop ends

return 0;
}

I hope the above answer solves your doubt.

Don't forget to give it a thumbs up.

Regards


Related Solutions

in java Write a while loop to ask the user to type number of hours(double) they...
in java Write a while loop to ask the user to type number of hours(double) they work per day. Calculate and print the salary for each valid input. If number of hours is greater than or equal to 0 and less than 5, then:  salary = numberofhours * 5, loop continues, the user can type another number If number of hours is greater or equal to 5, and less than 10, then: salary = numberofours * 8, loop continues, the user...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
C++ while loop Exercise Write a program that continues to ask the user to enter any...
C++ while loop Exercise Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total. #include <iostream> using namespace std; int main() { int number, n=0, sum=0; cout << "Enter a number to start...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to...
Python Programming Please! #Name: #Date: #Random number, loop while true #ask user for number. Check to see if the value is a number between 1 and 10 #if number is too high or too low, tell user, if they guessed it break out of loop Display "Welcome to my Guess the number program!" random mynumber count=1 while True try Display "Guess a number between 1 and 10"   Get guess while guess<1 or guess>10 Display "Guess a number between 1 and...
7. Ask the user to type in a number and compare this number with 8. The...
7. Ask the user to type in a number and compare this number with 8. The user could try twice. If the first user input was 8 then print out “Correct!” and terminate the program, otherwise ask user to type in a number for the second time. If the second input was 8 then print out “Correct”, otherwise print “Game over!” Finish the assembly code segment for the above requirement. .data message: .asciiz "Please enter a random number:" right_str: .asciiz...
Ask the user how many days that they collected gems. A loop you write should loop...
Ask the user how many days that they collected gems. A loop you write should loop how many days the user enters. If the user enters 5 days, then the loop should loop 5 times to collect the data from each day. So, each loop iteration represents a day. In each loop iteration, ask the user how many gems they collected that day. After the loop finishes gathering the data for each day, calculate the total and average gems collected....
Use a while(true) loop to ask the user the following 2 values “Enter a rate r...
Use a while(true) loop to ask the user the following 2 values “Enter a rate r =” “Enter a nonnegative integer (enter negative integer to quit):” If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the geometric series, namely 1 + r + rˆ2 + rˆ3 + . . . +rˆn. Use...
Use a while(true) loop to ask the user the following 2 values “Enter a value x...
Use a while(true) loop to ask the user the following 2 values “Enter a value x “ “Enter a nonnegative integer n (enter negative integer to quit): “ If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the partial sum for the Riemann zeta series for the geometric series, namely 1 + 2ˆ-x + 3ˆ-x +...
Inside a do while loop ask a user to enter two numbers (use a single scanf...
Inside a do while loop ask a user to enter two numbers (use a single scanf function). Inside the same loop ask the user whether they wish to find the min or max of the values entered. If the user enters a 0 call the minFunction; if the user enters a 1 call the maxFunction; otherwise print Not valid input. Pass the two first numbers entered as parameters to the functions. Inside minFunction and maxFunction calculate the minimun and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT