Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Worksheet 03 - Conditions

Conditions are used to make decisions in C as a selecting method.

If conditions:

 Syntax 01:
if(condition) {
//Code
}

 Syntax 02:
if(condition) {
//Code
} else
{
//Code
}

 Syntax 03:
if(condition) {
//Code
}
else if(condition)
{
//Code
} else
{
//Code
}
Relational Operators
Relational Operators are used in decision making and loops in C programming.
Operator Meaning
== Equal to
> Greater than
< Less than
!= Not equal to
>= Greater than or equal to
<= Less than or equal to

Logical Operators
Logical operators are used to combine expressions containing relation operators.
In C there are 3 logical operators.
Operator Meaning
&& Logical AND
|| Logical OR
! Logical NOT
Activities:

1. Write a program that reads the user’s age and prints:


* “You are a child”, if age is less than 18 and
* “You are a senior citizen”, if age is greater than or equals 18.

2. Write a program to show the grade of a student for the input marks. (The
program inputs the marks and show the grade)
Marks Grade
Greater than 75 (including 75) A
Greater than 55 (including 55) B
Greater than 40 (including 40) C
Below 40 (not including 40) D

3. Write a program that reads the user’s age and prints: * “You are a child”,
if age is less than 18.
* “You are an adult”, if is between 18 and 65(including 18 & 65).
* “You are a senior citizen”, if age is greater than 65.

4. Obtain a temperature in degrees Fahrenheit from the user. If the temperature


is 80 degrees or more display the message "Go play golf" otherwise, if the
temperature is 70 to 79 degrees display the message "Put on a jacket",
otherwise display the message "It is way too cold."

5. Write and run a program that simulates a simple calculator. It reads two
integers and a character (Try the same program in two different approaches).
* If the character is +, the sum is printed;
* If the character is -, the difference is printed;
* If the character is *, the product is printed;
* If the character is /, the quotient is printed and
* If the character is %, the remainder is printed

6. Try the same program in two different approaches.


7. Input the total number of electricity units consumed per month and calculate
the electricity bill according to the following criteria:
• for first 50 units Rs.0.50/unit
• for next 100 units Rs.0.75/unit
• for next 100 units Rs.1.20/unit
• for unit above 250 Rs.1.50/unit

8. Modify the above program to calculate the electricity bills of 10 people


depending on the number of electricity units the consumed.

9. Write a program to read a single character from the terminal and classify it
as an alphabetic character (a-z or A-Z), a digit (0-9) or a special character
(anything else).

10.A company insures its drivers in the following cases:


* If the driver is married
* If the driver is unmarried, male and above 30 years of age
* If the driver is unmarried, female and above 25 years of age
Read age, sex and marital status through the keyboard and output whether the
driver is insured or not.

You might also like