Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Introduction To Computing-LAB Lab 04: Topic Conditional Statement (If-Else) Objective Selection

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Introduction to Computing-LAB

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:

Enter marks of first Subject: 90


Enter marks of second Subject: 80
Enter marks of third Subject: 80

You’re above Standard!


Admission granted!

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:

Enter first number: 20


Enter second number: 30
Enter third number: 10

The maximum number is 30.

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.

Enter a number: -20


The number is negative.

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:

Enter first integer: 12


Enter second integer: 6

12 is the multiple of 6
Enter first integer: 12
Enter second integer: 13

12 is not the multiple of 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:

Enter ITC average for 6 sections:


B 90
D 80
C 60
A 99
E 91
F 80

A got the highest average

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:

Enter roll no and marks of student in each course


Roll# C1 C2 C3 C4 C5
1391 80 70 60 14 88
1372 77 90 90 99 100
1375 73 83 40 81 69

1372 has highest Aggregate of 456

Task 9
Write a program which takes 3 inputs integers and tell the 2nd maximum.
Sample Output:

Enter three integers: 90 5 60

Second maximum integer is 60

Task 10
Write a program which takes as input a floating number and prints its ceiling Integer.
Sample Output:

Enter floating-point number: 5.5


Its ceiling integer is 6

Enter floating-point number: -5.5


Its ceiling integer is -5

Enter floating-point number: 5


Its ceiling integer is 5

Task 11
Write a program which takes as input a floating number and prints its floor Integer.
Sample Output:

Enter floating-point number: 5.5


Its floor integer is 5

Enter floating-point number: -5.5


Its floor integer is -6

Enter floating-point number: 5


Its floor integer is 5
Task 12
Write a program of a BMI calculator application that reads the user’s weight and height and then calculates
and displays the user’s body mass index. The formula for calculating BMI is:

BMI = Weight / (Height * Height)

Also, the application should display the following information from the scale provided below.

BMI VALUES

Underweight: less than 18.5


Normal: between 18.5 and 24.9
Overweight: between 25 and 29.9
Obese: 30 or greater
Note: You are allowed to use Logical Operators and If statements.

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.

For weighing in Ounces

total_weight = number_of_chocolates * weight_of_Chocolate

For weighing in Pounds

total_weight = number_of_chocolates * weight_of_Chocolate / 16

For weighing in Grams

total_weight = number_of_chocolates*weight_of_Chocolate * 28.349

For weighing in Kilograms

total_weight = number_of_chocolates*weight_of_Chocolate*28.349/1000;

Sample Output:

Enter the number of chocolates being sold: 20


Enter weight of one chocolate in ounces: 2
Enter the choice for weighing
=> Enter O to calculate in ounces
=> P for pounds
=> G for grams
=> Kfor kilograms
Enter your Choice: O
Weight in Ounces is 40

You might also like