Introduction To Computing-LAB Lab 04: Topic Conditional Statement (If-Else) Objective Selection
Introduction To Computing-LAB Lab 04: Topic Conditional Statement (If-Else) Objective Selection
Introduction To Computing-LAB Lab 04: Topic Conditional Statement (If-Else) Objective Selection
Lab 04
Topic Conditional Statement (if-else)
Objective Learning how to build logic by using conditional statement.
Selection:
In selection, the program executes particular statements depending on some conditions. There are multiple
selection statements:
if:
syntax:
if(expression)
{
Body of if(statement);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If condition/expression
is false body of selection statement will be skipped and next instructions which are defined after selection
statement will be executed.
if else:
syntax:
if(expression)
{
Body of if(statement);
}
else
{
Body of else(statements);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If condition/expression
is false body of selection statement will be skipped and body of else will be executed. Else part doesn’t have
any condition/expression.
if else if:
syntax:
if(expression)
{
Body of if(statement);
}
else if(expression)
{
Body of else(statements);
}
In this selection statement a set of instruction are dependent on the condition/expression of selection
statement if condition/expression is true body of selection statement will be executed. If condition/expression
is false, then next selection statement’s condition/expression will be tested and so on. If any of the selection
statement is tested as true than no further statement will be test and the control of program will be shifted
after those selection statement.
Task 1: Write a program that inputs marks of three subjects. If the average of marks is more than 80. It displays
two messages “You’re above Standard!” and “Admission granted!”
Sample Output:
Task 2
Write a program that take a integer input from the user and tell whether the number is even or odd.
Sample Output:
Enter Number: 34
Number 34 is even.
Enter Number: 13
Number 34 is odd.
Enter letter: 26
Number 34 is even.
Task 3
Write a program that inputs three numbers and displays the maximum number.
Sample Output:
Task 4
Write a program to input a number from user and determine whether it is positive, negative or zero.
Sample Output:
Enter a number: 20
The number is positive.
Enter a number: 0
The number is zero.
Task 5
Write a program that reads two integers and then uses the conditional expression operator to print either
its “multiple” or “not” according to whether one of the integers is a multiple of the other.
Sample Output:
12 is the multiple of 6
Enter first integer: 12
Enter second integer: 13
Task 6
Write a program that simulates a simple calculator. It reads two integers and a character.
• If the character is a ‘+’, the sum is printed;
• if it is a ‘-‘, the difference is printed;
• if it is a ‘*’, the product is printed;
• if it is a ‘/’, the quotient is printed; and
• if it is a ‘%’, the remainder is printed.
Sample Output:
Input: 12%7
Modulus = 5
Input: 19x10
Multiplication = 190
Input: 12/7
Division = 1.7142
Task 7
ITC has 6 sections we are required to find out which sections average is higher. Write a program which
takes each section’s ITC average and Output which section has won w.r.t average.
Sample Output:
Task 8
Write a program which takes marks of 5 courses as input, of 5 students and output the students who got
the highest aggregate (which is total)
Sample Output:
Task 9
Write a program which takes 3 inputs integers and tell the 2nd maximum.
Sample Output:
Task 10
Write a program which takes as input a floating number and prints its ceiling Integer.
Sample Output:
Task 11
Write a program which takes as input a floating number and prints its floor Integer.
Sample Output:
Also, the application should display the following information from the scale provided below.
BMI VALUES
Task 13
Write a program to determine the weight of chocolates being sold. The program takes as input, the
number of chocolates being sold, weight of one chocolate in ounces and the choice of weighing i.e. ounces,
pounds, grams or kilograms. User enters ‘O’ as a choice to calculate weight in ounces, ‘P’ for pounds, ‘G’
for grams and ‘K’ for kilograms. Depending on the choice entered by the user the program calculates the
weight of chocolates. Display an appropriate message if invalid choice is entered. Use following formulas
to calculate total weight of chocolates.
total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000;
Sample Output: