Lab 4 PF
Lab 4 PF
Summary
Items Description
Course Title Programming Fundamentals
Lab Title Conditional Statement
Duration 3 Hours
Operating System Visual Studio
/Tool/Language
Objective To get familiar with if else and nested if else statements in C++
Syntax:
if ( condition )
{ //statement(s) to be executed if condition is true
}
else
{ //statement(s) to be executed if condition is not true / false
}
Sample program # 01
#include "stdafx.h"
#include<iostream>
using namespace std;
Sample program # 02
#include "stdafx.h"
#include<iostream>
using namespace std;
system("pause");
return 0;
}
Syntax # 02
(condition)? statement1_if_cond_true : statemnt2_if_cond_false ;
LAB TASKS
TASK # 01
Run both the sample programs, note the output and get familiar with the syntax
TASK # 02
Write a program which take a number from the user and display weather the number is even or odd
Sample Output:
Enter a number? 5
you have entered an odd integer
TASK # 03
Write a C++ code which take three inputs from user, your program should display the smallest value (use
nested if )
Sample Output:
Enter 1st value = 10
Enter 2nd value = 2
Enter 3rd value = 5
Smallest number = 2
TASK # 04
Write a program which take 4 unique inputs from the user and find the largest number from them.
(use logical operators for multiple conditions)
Sample Output:
Enter 1st value? 5
Enter 2nd value? 9
Enter 3rd value? 10
Enter 4th value? 3
Largest number = 10
TASK # 05
Create a calculator in C++ which can perform addition, subtraction, multiplication, division
Ask the user to enter the operator first
then ask the user to enter first and then second value
Sample Output:
Please enter the operator which you want to perform? +
please enter first value? 5
Please enter second value? 6
5+6 =11
TASK # 06
Write a program to check whether a triangle is valid or not. The three angles of the triangle are
entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180
degrees.
Use Syntax # 02 of if-else statement for this task
Task # 07
A library charges a fine for every book returned late. For first 7 days late the fine is 10 PKR, for 8-14
days fine is 20PKR and for 15-31 days fine is 50PKR. If you return the book after 31 days your
membership will be cancelled. Write a program to accept the number of days from the user and
display the fine message or the appropriate message.