0% found this document useful (0 votes)
37 views

Lab 4 PF

The document describes a lab assignment on conditional statements in C++. It includes the objective to get familiar with if/else and nested if/else statements. It provides syntax examples and sample programs using if/else statements to compare numbers. It then lists 7 tasks for students to complete that involve writing C++ programs using conditional logic, including determining even/odd numbers, finding minimum/maximum numbers, and calculating fines based on number of late days.

Uploaded by

maham sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Lab 4 PF

The document describes a lab assignment on conditional statements in C++. It includes the objective to get familiar with if/else and nested if/else statements. It provides syntax examples and sample programs using if/else statements to compare numbers. It then lists 7 tasks for students to complete that involve writing C++ programs using conditional logic, including determining even/odd numbers, finding minimum/maximum numbers, and calculating fines based on number of late days.

Uploaded by

maham sabir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

LAB 04

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
}

Condition is a statement whose result is 0/1 0r True/False or Yes / No


can contain expression / mathematical operator(s) logical operator(s) Assignment operator.
Condition can contain more than one condition

Sample program # 01

#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{cout<<"use of if else statement"<<endl;
int a,b;
cout<<"enter 1st number ?"<<endl;
cin>>a;
cout<<"enter second number?"<<endl;
cin>>b;
if(a>b)
{cout<<" 1st # is > 2nd #"<<endl; }
else
cout<<"2nd no is greater than first"<<endl;
system("pause");
return 0;
}

Sample program # 02
#include "stdafx.h"
#include<iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])


{
cout<<"use of nested if"<<endl;
int a=100,b=200,c=3000;
if(a>b)
{ if(a>c) // (a>b) (a>c)
cout<<a<<"is largest "<<endl;
else if (c>b) // (a>b)
cout<<c<<"is largest"<<endl;
}
else // (b>a)
{
if (b>c) // (b>a)
cout<<b<<"is largest"<<endl;
else //(b>a) (c>b)
cout<<c<<" is largest"<<endl;
}

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.

You might also like