In: Computer Science
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.
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