136k views
2 votes
Assume that a boolean variable workedOvertime has been declared, and that another variable, hoursWorked has been declared and initialized. Write a statement that assigns the value true to workedOvertime if hoursWorked is greater than 40 and false otherwise.

User Umut
by
4.7k points

1 Answer

4 votes

Answer:

The c++ program to show the use of Boolean variable is given below.

#include <iostream>

using namespace std;

int main() {

int hoursWorked = 44;

bool workedOvertime;

cout<<"This program shows the use of boolean variable." <<endl;

// overtime refers to working hours more than 40 hours

if(hoursWorked > 40)

{

workedOvertime = true;

cout<<"The value of boolean variable workedOvertime is true or "<<workedOvertime<<endl;

}

// if working hours are less than or equal to 40, overtime is not considered

else

{

workedOvertime = false;

cout<<"The value of boolean variable workedOvertime is false or "<<workedOvertime<<endl;

}

return 0;

}

OUTPUT

This program shows the use of boolean variable.

The value of boolean variable workedOvertime is true or 1

Step-by-step explanation:

This program uses a boolean variable to indicate the overtime of working hours.

The boolean variable is declared as shown.

bool workedOvertime;

The working hours is stored in an integer variable which is declared and initialized within the program itself.

int hoursWorked = 44;

Depending on the value of working hours, the boolean variable is assigned the value true or false.

if(hoursWorked > 40)

{

workedOvertime = true;

cout<<"The value of boolean variable workedOvertime is true or "<<workedOvertime<<endl;

}

else

{

workedOvertime = false;

cout<<"The value of boolean variable workedOvertime is false or "<<workedOvertime<<endl;

}

The value of boolean variable is displayed to the user.

In the above program, the working hours are 44 which is more than normal working hours of 40. Hence, boolean variable is assigned value true.

This program does not accept user input for working hours as stated in the question. Since value of working hours is not accepted from the user, there is no logic implemented to check the validity of the input.

This program can be tested for different values of working hours to test for the boolean variable.

User Alex Parloti
by
4.5k points